intel_display.c 257 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613
  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_ratio(uint32_t *num, uint32_t *den)
  3487. {
  3488. while (*num > 0xffffff || *den > 0xffffff) {
  3489. *num >>= 1;
  3490. *den >>= 1;
  3491. }
  3492. }
  3493. void
  3494. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  3495. int pixel_clock, int link_clock,
  3496. struct intel_link_m_n *m_n)
  3497. {
  3498. m_n->tu = 64;
  3499. m_n->gmch_m = bits_per_pixel * pixel_clock;
  3500. m_n->gmch_n = link_clock * nlanes * 8;
  3501. intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  3502. m_n->link_m = pixel_clock;
  3503. m_n->link_n = link_clock;
  3504. intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
  3505. }
  3506. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  3507. {
  3508. if (i915_panel_use_ssc >= 0)
  3509. return i915_panel_use_ssc != 0;
  3510. return dev_priv->lvds_use_ssc
  3511. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  3512. }
  3513. static int vlv_get_refclk(struct drm_crtc *crtc)
  3514. {
  3515. struct drm_device *dev = crtc->dev;
  3516. struct drm_i915_private *dev_priv = dev->dev_private;
  3517. int refclk = 27000; /* for DP & HDMI */
  3518. return 100000; /* only one validated so far */
  3519. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  3520. refclk = 96000;
  3521. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  3522. if (intel_panel_use_ssc(dev_priv))
  3523. refclk = 100000;
  3524. else
  3525. refclk = 96000;
  3526. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
  3527. refclk = 100000;
  3528. }
  3529. return refclk;
  3530. }
  3531. static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
  3532. {
  3533. struct drm_device *dev = crtc->dev;
  3534. struct drm_i915_private *dev_priv = dev->dev_private;
  3535. int refclk;
  3536. if (IS_VALLEYVIEW(dev)) {
  3537. refclk = vlv_get_refclk(crtc);
  3538. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  3539. intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  3540. refclk = dev_priv->lvds_ssc_freq * 1000;
  3541. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3542. refclk / 1000);
  3543. } else if (!IS_GEN2(dev)) {
  3544. refclk = 96000;
  3545. } else {
  3546. refclk = 48000;
  3547. }
  3548. return refclk;
  3549. }
  3550. static void i9xx_adjust_sdvo_tv_clock(struct intel_crtc *crtc)
  3551. {
  3552. unsigned dotclock = crtc->config.adjusted_mode.clock;
  3553. struct dpll *clock = &crtc->config.dpll;
  3554. /* SDVO TV has fixed PLL values depend on its clock range,
  3555. this mirrors vbios setting. */
  3556. if (dotclock >= 100000 && dotclock < 140500) {
  3557. clock->p1 = 2;
  3558. clock->p2 = 10;
  3559. clock->n = 3;
  3560. clock->m1 = 16;
  3561. clock->m2 = 8;
  3562. } else if (dotclock >= 140500 && dotclock <= 200000) {
  3563. clock->p1 = 1;
  3564. clock->p2 = 10;
  3565. clock->n = 6;
  3566. clock->m1 = 12;
  3567. clock->m2 = 8;
  3568. }
  3569. crtc->config.clock_set = true;
  3570. }
  3571. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  3572. intel_clock_t *reduced_clock)
  3573. {
  3574. struct drm_device *dev = crtc->base.dev;
  3575. struct drm_i915_private *dev_priv = dev->dev_private;
  3576. int pipe = crtc->pipe;
  3577. u32 fp, fp2 = 0;
  3578. struct dpll *clock = &crtc->config.dpll;
  3579. if (IS_PINEVIEW(dev)) {
  3580. fp = (1 << clock->n) << 16 | clock->m1 << 8 | clock->m2;
  3581. if (reduced_clock)
  3582. fp2 = (1 << reduced_clock->n) << 16 |
  3583. reduced_clock->m1 << 8 | reduced_clock->m2;
  3584. } else {
  3585. fp = clock->n << 16 | clock->m1 << 8 | clock->m2;
  3586. if (reduced_clock)
  3587. fp2 = reduced_clock->n << 16 | reduced_clock->m1 << 8 |
  3588. reduced_clock->m2;
  3589. }
  3590. I915_WRITE(FP0(pipe), fp);
  3591. crtc->lowfreq_avail = false;
  3592. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3593. reduced_clock && i915_powersave) {
  3594. I915_WRITE(FP1(pipe), fp2);
  3595. crtc->lowfreq_avail = true;
  3596. } else {
  3597. I915_WRITE(FP1(pipe), fp);
  3598. }
  3599. }
  3600. static void intel_dp_set_m_n(struct intel_crtc *crtc)
  3601. {
  3602. if (crtc->config.has_pch_encoder)
  3603. intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  3604. else
  3605. intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
  3606. }
  3607. static void vlv_update_pll(struct intel_crtc *crtc)
  3608. {
  3609. struct drm_device *dev = crtc->base.dev;
  3610. struct drm_i915_private *dev_priv = dev->dev_private;
  3611. int pipe = crtc->pipe;
  3612. u32 dpll, mdiv, pdiv;
  3613. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  3614. bool is_sdvo;
  3615. u32 temp;
  3616. mutex_lock(&dev_priv->dpio_lock);
  3617. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  3618. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  3619. dpll = DPLL_VGA_MODE_DIS;
  3620. dpll |= DPLL_EXT_BUFFER_ENABLE_VLV;
  3621. dpll |= DPLL_REFA_CLK_ENABLE_VLV;
  3622. dpll |= DPLL_INTEGRATED_CLOCK_VLV;
  3623. I915_WRITE(DPLL(pipe), dpll);
  3624. POSTING_READ(DPLL(pipe));
  3625. bestn = crtc->config.dpll.n;
  3626. bestm1 = crtc->config.dpll.m1;
  3627. bestm2 = crtc->config.dpll.m2;
  3628. bestp1 = crtc->config.dpll.p1;
  3629. bestp2 = crtc->config.dpll.p2;
  3630. /*
  3631. * In Valleyview PLL and program lane counter registers are exposed
  3632. * through DPIO interface
  3633. */
  3634. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  3635. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  3636. mdiv |= ((bestn << DPIO_N_SHIFT));
  3637. mdiv |= (1 << DPIO_POST_DIV_SHIFT);
  3638. mdiv |= (1 << DPIO_K_SHIFT);
  3639. mdiv |= DPIO_ENABLE_CALIBRATION;
  3640. intel_dpio_write(dev_priv, DPIO_DIV(pipe), mdiv);
  3641. intel_dpio_write(dev_priv, DPIO_CORE_CLK(pipe), 0x01000000);
  3642. pdiv = (1 << DPIO_REFSEL_OVERRIDE) | (5 << DPIO_PLL_MODESEL_SHIFT) |
  3643. (3 << DPIO_BIAS_CURRENT_CTL_SHIFT) | (1<<20) |
  3644. (7 << DPIO_PLL_REFCLK_SEL_SHIFT) | (8 << DPIO_DRIVER_CTL_SHIFT) |
  3645. (5 << DPIO_CLK_BIAS_CTL_SHIFT);
  3646. intel_dpio_write(dev_priv, DPIO_REFSFR(pipe), pdiv);
  3647. intel_dpio_write(dev_priv, DPIO_LFP_COEFF(pipe), 0x005f003b);
  3648. dpll |= DPLL_VCO_ENABLE;
  3649. I915_WRITE(DPLL(pipe), dpll);
  3650. POSTING_READ(DPLL(pipe));
  3651. if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
  3652. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  3653. intel_dpio_write(dev_priv, DPIO_FASTCLK_DISABLE, 0x620);
  3654. if (crtc->config.has_dp_encoder)
  3655. intel_dp_set_m_n(crtc);
  3656. I915_WRITE(DPLL(pipe), dpll);
  3657. /* Wait for the clocks to stabilize. */
  3658. POSTING_READ(DPLL(pipe));
  3659. udelay(150);
  3660. temp = 0;
  3661. if (is_sdvo) {
  3662. temp = 0;
  3663. if (crtc->config.pixel_multiplier > 1) {
  3664. temp = (crtc->config.pixel_multiplier - 1)
  3665. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3666. }
  3667. }
  3668. I915_WRITE(DPLL_MD(pipe), temp);
  3669. POSTING_READ(DPLL_MD(pipe));
  3670. /* Now program lane control registers */
  3671. if(intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)
  3672. || intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
  3673. temp = 0x1000C4;
  3674. if(pipe == 1)
  3675. temp |= (1 << 21);
  3676. intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL1, temp);
  3677. }
  3678. if(intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP)) {
  3679. temp = 0x1000C4;
  3680. if(pipe == 1)
  3681. temp |= (1 << 21);
  3682. intel_dpio_write(dev_priv, DPIO_DATA_CHANNEL2, temp);
  3683. }
  3684. mutex_unlock(&dev_priv->dpio_lock);
  3685. }
  3686. static void i9xx_update_pll(struct intel_crtc *crtc,
  3687. intel_clock_t *reduced_clock,
  3688. int num_connectors)
  3689. {
  3690. struct drm_device *dev = crtc->base.dev;
  3691. struct drm_i915_private *dev_priv = dev->dev_private;
  3692. struct intel_encoder *encoder;
  3693. int pipe = crtc->pipe;
  3694. u32 dpll;
  3695. bool is_sdvo;
  3696. struct dpll *clock = &crtc->config.dpll;
  3697. i9xx_update_pll_dividers(crtc, reduced_clock);
  3698. is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
  3699. intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
  3700. dpll = DPLL_VGA_MODE_DIS;
  3701. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
  3702. dpll |= DPLLB_MODE_LVDS;
  3703. else
  3704. dpll |= DPLLB_MODE_DAC_SERIAL;
  3705. if (is_sdvo) {
  3706. if ((crtc->config.pixel_multiplier > 1) &&
  3707. (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))) {
  3708. dpll |= (crtc->config.pixel_multiplier - 1)
  3709. << SDVO_MULTIPLIER_SHIFT_HIRES;
  3710. }
  3711. dpll |= DPLL_DVO_HIGH_SPEED;
  3712. }
  3713. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
  3714. dpll |= DPLL_DVO_HIGH_SPEED;
  3715. /* compute bitmask from p1 value */
  3716. if (IS_PINEVIEW(dev))
  3717. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3718. else {
  3719. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3720. if (IS_G4X(dev) && reduced_clock)
  3721. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3722. }
  3723. switch (clock->p2) {
  3724. case 5:
  3725. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3726. break;
  3727. case 7:
  3728. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3729. break;
  3730. case 10:
  3731. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3732. break;
  3733. case 14:
  3734. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3735. break;
  3736. }
  3737. if (INTEL_INFO(dev)->gen >= 4)
  3738. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3739. if (is_sdvo && intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_TVOUT))
  3740. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3741. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_TVOUT))
  3742. /* XXX: just matching BIOS for now */
  3743. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3744. dpll |= 3;
  3745. else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3746. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3747. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3748. else
  3749. dpll |= PLL_REF_INPUT_DREFCLK;
  3750. dpll |= DPLL_VCO_ENABLE;
  3751. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3752. POSTING_READ(DPLL(pipe));
  3753. udelay(150);
  3754. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  3755. if (encoder->pre_pll_enable)
  3756. encoder->pre_pll_enable(encoder);
  3757. if (crtc->config.has_dp_encoder)
  3758. intel_dp_set_m_n(crtc);
  3759. I915_WRITE(DPLL(pipe), dpll);
  3760. /* Wait for the clocks to stabilize. */
  3761. POSTING_READ(DPLL(pipe));
  3762. udelay(150);
  3763. if (INTEL_INFO(dev)->gen >= 4) {
  3764. u32 temp = 0;
  3765. if (is_sdvo) {
  3766. temp = 0;
  3767. if (crtc->config.pixel_multiplier > 1) {
  3768. temp = (crtc->config.pixel_multiplier - 1)
  3769. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  3770. }
  3771. }
  3772. I915_WRITE(DPLL_MD(pipe), temp);
  3773. } else {
  3774. /* The pixel multiplier can only be updated once the
  3775. * DPLL is enabled and the clocks are stable.
  3776. *
  3777. * So write it again.
  3778. */
  3779. I915_WRITE(DPLL(pipe), dpll);
  3780. }
  3781. }
  3782. static void i8xx_update_pll(struct intel_crtc *crtc,
  3783. struct drm_display_mode *adjusted_mode,
  3784. intel_clock_t *reduced_clock,
  3785. int num_connectors)
  3786. {
  3787. struct drm_device *dev = crtc->base.dev;
  3788. struct drm_i915_private *dev_priv = dev->dev_private;
  3789. struct intel_encoder *encoder;
  3790. int pipe = crtc->pipe;
  3791. u32 dpll;
  3792. struct dpll *clock = &crtc->config.dpll;
  3793. i9xx_update_pll_dividers(crtc, reduced_clock);
  3794. dpll = DPLL_VGA_MODE_DIS;
  3795. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
  3796. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3797. } else {
  3798. if (clock->p1 == 2)
  3799. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3800. else
  3801. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3802. if (clock->p2 == 4)
  3803. dpll |= PLL_P2_DIVIDE_BY_4;
  3804. }
  3805. if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
  3806. intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  3807. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3808. else
  3809. dpll |= PLL_REF_INPUT_DREFCLK;
  3810. dpll |= DPLL_VCO_ENABLE;
  3811. I915_WRITE(DPLL(pipe), dpll & ~DPLL_VCO_ENABLE);
  3812. POSTING_READ(DPLL(pipe));
  3813. udelay(150);
  3814. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  3815. if (encoder->pre_pll_enable)
  3816. encoder->pre_pll_enable(encoder);
  3817. I915_WRITE(DPLL(pipe), dpll);
  3818. /* Wait for the clocks to stabilize. */
  3819. POSTING_READ(DPLL(pipe));
  3820. udelay(150);
  3821. /* The pixel multiplier can only be updated once the
  3822. * DPLL is enabled and the clocks are stable.
  3823. *
  3824. * So write it again.
  3825. */
  3826. I915_WRITE(DPLL(pipe), dpll);
  3827. }
  3828. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc,
  3829. struct drm_display_mode *mode,
  3830. struct drm_display_mode *adjusted_mode)
  3831. {
  3832. struct drm_device *dev = intel_crtc->base.dev;
  3833. struct drm_i915_private *dev_priv = dev->dev_private;
  3834. enum pipe pipe = intel_crtc->pipe;
  3835. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  3836. uint32_t vsyncshift;
  3837. if (!IS_GEN2(dev) && adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3838. /* the chip adds 2 halflines automatically */
  3839. adjusted_mode->crtc_vtotal -= 1;
  3840. adjusted_mode->crtc_vblank_end -= 1;
  3841. vsyncshift = adjusted_mode->crtc_hsync_start
  3842. - adjusted_mode->crtc_htotal / 2;
  3843. } else {
  3844. vsyncshift = 0;
  3845. }
  3846. if (INTEL_INFO(dev)->gen > 3)
  3847. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  3848. I915_WRITE(HTOTAL(cpu_transcoder),
  3849. (adjusted_mode->crtc_hdisplay - 1) |
  3850. ((adjusted_mode->crtc_htotal - 1) << 16));
  3851. I915_WRITE(HBLANK(cpu_transcoder),
  3852. (adjusted_mode->crtc_hblank_start - 1) |
  3853. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3854. I915_WRITE(HSYNC(cpu_transcoder),
  3855. (adjusted_mode->crtc_hsync_start - 1) |
  3856. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3857. I915_WRITE(VTOTAL(cpu_transcoder),
  3858. (adjusted_mode->crtc_vdisplay - 1) |
  3859. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3860. I915_WRITE(VBLANK(cpu_transcoder),
  3861. (adjusted_mode->crtc_vblank_start - 1) |
  3862. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3863. I915_WRITE(VSYNC(cpu_transcoder),
  3864. (adjusted_mode->crtc_vsync_start - 1) |
  3865. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3866. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  3867. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  3868. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  3869. * bits. */
  3870. if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
  3871. (pipe == PIPE_B || pipe == PIPE_C))
  3872. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  3873. /* pipesrc controls the size that is scaled from, which should
  3874. * always be the user's requested size.
  3875. */
  3876. I915_WRITE(PIPESRC(pipe),
  3877. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3878. }
  3879. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  3880. {
  3881. struct drm_device *dev = intel_crtc->base.dev;
  3882. struct drm_i915_private *dev_priv = dev->dev_private;
  3883. uint32_t pipeconf;
  3884. pipeconf = I915_READ(PIPECONF(intel_crtc->pipe));
  3885. if (intel_crtc->pipe == 0 && INTEL_INFO(dev)->gen < 4) {
  3886. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3887. * core speed.
  3888. *
  3889. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3890. * pipe == 0 check?
  3891. */
  3892. if (intel_crtc->config.requested_mode.clock >
  3893. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3894. pipeconf |= PIPECONF_DOUBLE_WIDE;
  3895. else
  3896. pipeconf &= ~PIPECONF_DOUBLE_WIDE;
  3897. }
  3898. /* default to 8bpc */
  3899. pipeconf &= ~(PIPECONF_BPC_MASK | PIPECONF_DITHER_EN);
  3900. if (intel_crtc->config.has_dp_encoder) {
  3901. if (intel_crtc->config.dither) {
  3902. pipeconf |= PIPECONF_6BPC |
  3903. PIPECONF_DITHER_EN |
  3904. PIPECONF_DITHER_TYPE_SP;
  3905. }
  3906. }
  3907. if (IS_VALLEYVIEW(dev) && intel_pipe_has_type(&intel_crtc->base,
  3908. INTEL_OUTPUT_EDP)) {
  3909. if (intel_crtc->config.dither) {
  3910. pipeconf |= PIPECONF_6BPC |
  3911. PIPECONF_ENABLE |
  3912. I965_PIPECONF_ACTIVE;
  3913. }
  3914. }
  3915. if (HAS_PIPE_CXSR(dev)) {
  3916. if (intel_crtc->lowfreq_avail) {
  3917. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3918. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3919. } else {
  3920. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3921. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3922. }
  3923. }
  3924. pipeconf &= ~PIPECONF_INTERLACE_MASK;
  3925. if (!IS_GEN2(dev) &&
  3926. intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  3927. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3928. else
  3929. pipeconf |= PIPECONF_PROGRESSIVE;
  3930. if (IS_VALLEYVIEW(dev)) {
  3931. if (intel_crtc->config.limited_color_range)
  3932. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  3933. else
  3934. pipeconf &= ~PIPECONF_COLOR_RANGE_SELECT;
  3935. }
  3936. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  3937. POSTING_READ(PIPECONF(intel_crtc->pipe));
  3938. }
  3939. static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
  3940. int x, int y,
  3941. struct drm_framebuffer *fb)
  3942. {
  3943. struct drm_device *dev = crtc->dev;
  3944. struct drm_i915_private *dev_priv = dev->dev_private;
  3945. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3946. struct drm_display_mode *adjusted_mode =
  3947. &intel_crtc->config.adjusted_mode;
  3948. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  3949. int pipe = intel_crtc->pipe;
  3950. int plane = intel_crtc->plane;
  3951. int refclk, num_connectors = 0;
  3952. intel_clock_t clock, reduced_clock;
  3953. u32 dspcntr;
  3954. bool ok, has_reduced_clock = false, is_sdvo = false;
  3955. bool is_lvds = false, is_tv = false;
  3956. struct intel_encoder *encoder;
  3957. const intel_limit_t *limit;
  3958. int ret;
  3959. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3960. switch (encoder->type) {
  3961. case INTEL_OUTPUT_LVDS:
  3962. is_lvds = true;
  3963. break;
  3964. case INTEL_OUTPUT_SDVO:
  3965. case INTEL_OUTPUT_HDMI:
  3966. is_sdvo = true;
  3967. if (encoder->needs_tv_clock)
  3968. is_tv = true;
  3969. break;
  3970. case INTEL_OUTPUT_TVOUT:
  3971. is_tv = true;
  3972. break;
  3973. }
  3974. num_connectors++;
  3975. }
  3976. refclk = i9xx_get_refclk(crtc, num_connectors);
  3977. /*
  3978. * Returns a set of divisors for the desired target clock with the given
  3979. * refclk, or FALSE. The returned values represent the clock equation:
  3980. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3981. */
  3982. limit = intel_limit(crtc, refclk);
  3983. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  3984. &clock);
  3985. if (!ok) {
  3986. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3987. return -EINVAL;
  3988. }
  3989. /* Ensure that the cursor is valid for the new mode before changing... */
  3990. intel_crtc_update_cursor(crtc, true);
  3991. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3992. /*
  3993. * Ensure we match the reduced clock's P to the target clock.
  3994. * If the clocks don't match, we can't switch the display clock
  3995. * by using the FP0/FP1. In such case we will disable the LVDS
  3996. * downclock feature.
  3997. */
  3998. has_reduced_clock = limit->find_pll(limit, crtc,
  3999. dev_priv->lvds_downclock,
  4000. refclk,
  4001. &clock,
  4002. &reduced_clock);
  4003. }
  4004. /* Compat-code for transition, will disappear. */
  4005. if (!intel_crtc->config.clock_set) {
  4006. intel_crtc->config.dpll.n = clock.n;
  4007. intel_crtc->config.dpll.m1 = clock.m1;
  4008. intel_crtc->config.dpll.m2 = clock.m2;
  4009. intel_crtc->config.dpll.p1 = clock.p1;
  4010. intel_crtc->config.dpll.p2 = clock.p2;
  4011. }
  4012. if (is_sdvo && is_tv)
  4013. i9xx_adjust_sdvo_tv_clock(intel_crtc);
  4014. if (IS_GEN2(dev))
  4015. i8xx_update_pll(intel_crtc, adjusted_mode,
  4016. has_reduced_clock ? &reduced_clock : NULL,
  4017. num_connectors);
  4018. else if (IS_VALLEYVIEW(dev))
  4019. vlv_update_pll(intel_crtc);
  4020. else
  4021. i9xx_update_pll(intel_crtc,
  4022. has_reduced_clock ? &reduced_clock : NULL,
  4023. num_connectors);
  4024. /* Set up the display plane register */
  4025. dspcntr = DISPPLANE_GAMMA_ENABLE;
  4026. if (!IS_VALLEYVIEW(dev)) {
  4027. if (pipe == 0)
  4028. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  4029. else
  4030. dspcntr |= DISPPLANE_SEL_PIPE_B;
  4031. }
  4032. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  4033. drm_mode_debug_printmodeline(mode);
  4034. intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
  4035. /* pipesrc and dspsize control the size that is scaled from,
  4036. * which should always be the user's requested size.
  4037. */
  4038. I915_WRITE(DSPSIZE(plane),
  4039. ((mode->vdisplay - 1) << 16) |
  4040. (mode->hdisplay - 1));
  4041. I915_WRITE(DSPPOS(plane), 0);
  4042. i9xx_set_pipeconf(intel_crtc);
  4043. intel_enable_pipe(dev_priv, pipe, false);
  4044. intel_wait_for_vblank(dev, pipe);
  4045. I915_WRITE(DSPCNTR(plane), dspcntr);
  4046. POSTING_READ(DSPCNTR(plane));
  4047. ret = intel_pipe_set_base(crtc, x, y, fb);
  4048. intel_update_watermarks(dev);
  4049. return ret;
  4050. }
  4051. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  4052. struct intel_crtc_config *pipe_config)
  4053. {
  4054. struct drm_device *dev = crtc->base.dev;
  4055. struct drm_i915_private *dev_priv = dev->dev_private;
  4056. uint32_t tmp;
  4057. tmp = I915_READ(PIPECONF(crtc->pipe));
  4058. if (!(tmp & PIPECONF_ENABLE))
  4059. return false;
  4060. return true;
  4061. }
  4062. static void ironlake_init_pch_refclk(struct drm_device *dev)
  4063. {
  4064. struct drm_i915_private *dev_priv = dev->dev_private;
  4065. struct drm_mode_config *mode_config = &dev->mode_config;
  4066. struct intel_encoder *encoder;
  4067. u32 val, final;
  4068. bool has_lvds = false;
  4069. bool has_cpu_edp = false;
  4070. bool has_pch_edp = false;
  4071. bool has_panel = false;
  4072. bool has_ck505 = false;
  4073. bool can_ssc = false;
  4074. /* We need to take the global config into account */
  4075. list_for_each_entry(encoder, &mode_config->encoder_list,
  4076. base.head) {
  4077. switch (encoder->type) {
  4078. case INTEL_OUTPUT_LVDS:
  4079. has_panel = true;
  4080. has_lvds = true;
  4081. break;
  4082. case INTEL_OUTPUT_EDP:
  4083. has_panel = true;
  4084. if (intel_encoder_is_pch_edp(&encoder->base))
  4085. has_pch_edp = true;
  4086. else
  4087. has_cpu_edp = true;
  4088. break;
  4089. }
  4090. }
  4091. if (HAS_PCH_IBX(dev)) {
  4092. has_ck505 = dev_priv->display_clock_mode;
  4093. can_ssc = has_ck505;
  4094. } else {
  4095. has_ck505 = false;
  4096. can_ssc = true;
  4097. }
  4098. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_pch_edp %d has_cpu_edp %d has_ck505 %d\n",
  4099. has_panel, has_lvds, has_pch_edp, has_cpu_edp,
  4100. has_ck505);
  4101. /* Ironlake: try to setup display ref clock before DPLL
  4102. * enabling. This is only under driver's control after
  4103. * PCH B stepping, previous chipset stepping should be
  4104. * ignoring this setting.
  4105. */
  4106. val = I915_READ(PCH_DREF_CONTROL);
  4107. /* As we must carefully and slowly disable/enable each source in turn,
  4108. * compute the final state we want first and check if we need to
  4109. * make any changes at all.
  4110. */
  4111. final = val;
  4112. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  4113. if (has_ck505)
  4114. final |= DREF_NONSPREAD_CK505_ENABLE;
  4115. else
  4116. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  4117. final &= ~DREF_SSC_SOURCE_MASK;
  4118. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4119. final &= ~DREF_SSC1_ENABLE;
  4120. if (has_panel) {
  4121. final |= DREF_SSC_SOURCE_ENABLE;
  4122. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4123. final |= DREF_SSC1_ENABLE;
  4124. if (has_cpu_edp) {
  4125. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  4126. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4127. else
  4128. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4129. } else
  4130. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4131. } else {
  4132. final |= DREF_SSC_SOURCE_DISABLE;
  4133. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4134. }
  4135. if (final == val)
  4136. return;
  4137. /* Always enable nonspread source */
  4138. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  4139. if (has_ck505)
  4140. val |= DREF_NONSPREAD_CK505_ENABLE;
  4141. else
  4142. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  4143. if (has_panel) {
  4144. val &= ~DREF_SSC_SOURCE_MASK;
  4145. val |= DREF_SSC_SOURCE_ENABLE;
  4146. /* SSC must be turned on before enabling the CPU output */
  4147. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4148. DRM_DEBUG_KMS("Using SSC on panel\n");
  4149. val |= DREF_SSC1_ENABLE;
  4150. } else
  4151. val &= ~DREF_SSC1_ENABLE;
  4152. /* Get SSC going before enabling the outputs */
  4153. I915_WRITE(PCH_DREF_CONTROL, val);
  4154. POSTING_READ(PCH_DREF_CONTROL);
  4155. udelay(200);
  4156. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4157. /* Enable CPU source on CPU attached eDP */
  4158. if (has_cpu_edp) {
  4159. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  4160. DRM_DEBUG_KMS("Using SSC on eDP\n");
  4161. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  4162. }
  4163. else
  4164. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  4165. } else
  4166. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4167. I915_WRITE(PCH_DREF_CONTROL, val);
  4168. POSTING_READ(PCH_DREF_CONTROL);
  4169. udelay(200);
  4170. } else {
  4171. DRM_DEBUG_KMS("Disabling SSC entirely\n");
  4172. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  4173. /* Turn off CPU output */
  4174. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  4175. I915_WRITE(PCH_DREF_CONTROL, val);
  4176. POSTING_READ(PCH_DREF_CONTROL);
  4177. udelay(200);
  4178. /* Turn off the SSC source */
  4179. val &= ~DREF_SSC_SOURCE_MASK;
  4180. val |= DREF_SSC_SOURCE_DISABLE;
  4181. /* Turn off SSC1 */
  4182. val &= ~DREF_SSC1_ENABLE;
  4183. I915_WRITE(PCH_DREF_CONTROL, val);
  4184. POSTING_READ(PCH_DREF_CONTROL);
  4185. udelay(200);
  4186. }
  4187. BUG_ON(val != final);
  4188. }
  4189. /* Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O. */
  4190. static void lpt_init_pch_refclk(struct drm_device *dev)
  4191. {
  4192. struct drm_i915_private *dev_priv = dev->dev_private;
  4193. struct drm_mode_config *mode_config = &dev->mode_config;
  4194. struct intel_encoder *encoder;
  4195. bool has_vga = false;
  4196. bool is_sdv = false;
  4197. u32 tmp;
  4198. list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
  4199. switch (encoder->type) {
  4200. case INTEL_OUTPUT_ANALOG:
  4201. has_vga = true;
  4202. break;
  4203. }
  4204. }
  4205. if (!has_vga)
  4206. return;
  4207. mutex_lock(&dev_priv->dpio_lock);
  4208. /* XXX: Rip out SDV support once Haswell ships for real. */
  4209. if (IS_HASWELL(dev) && (dev->pci_device & 0xFF00) == 0x0C00)
  4210. is_sdv = true;
  4211. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4212. tmp &= ~SBI_SSCCTL_DISABLE;
  4213. tmp |= SBI_SSCCTL_PATHALT;
  4214. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4215. udelay(24);
  4216. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  4217. tmp &= ~SBI_SSCCTL_PATHALT;
  4218. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  4219. if (!is_sdv) {
  4220. tmp = I915_READ(SOUTH_CHICKEN2);
  4221. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  4222. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4223. if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
  4224. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  4225. DRM_ERROR("FDI mPHY reset assert timeout\n");
  4226. tmp = I915_READ(SOUTH_CHICKEN2);
  4227. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  4228. I915_WRITE(SOUTH_CHICKEN2, tmp);
  4229. if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
  4230. FDI_MPHY_IOSFSB_RESET_STATUS) == 0,
  4231. 100))
  4232. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  4233. }
  4234. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  4235. tmp &= ~(0xFF << 24);
  4236. tmp |= (0x12 << 24);
  4237. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  4238. if (is_sdv) {
  4239. tmp = intel_sbi_read(dev_priv, 0x800C, SBI_MPHY);
  4240. tmp |= 0x7FFF;
  4241. intel_sbi_write(dev_priv, 0x800C, tmp, SBI_MPHY);
  4242. }
  4243. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  4244. tmp |= (1 << 11);
  4245. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  4246. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  4247. tmp |= (1 << 11);
  4248. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  4249. if (is_sdv) {
  4250. tmp = intel_sbi_read(dev_priv, 0x2038, SBI_MPHY);
  4251. tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
  4252. intel_sbi_write(dev_priv, 0x2038, tmp, SBI_MPHY);
  4253. tmp = intel_sbi_read(dev_priv, 0x2138, SBI_MPHY);
  4254. tmp |= (0x3F << 24) | (0xF << 20) | (0xF << 16);
  4255. intel_sbi_write(dev_priv, 0x2138, tmp, SBI_MPHY);
  4256. tmp = intel_sbi_read(dev_priv, 0x203C, SBI_MPHY);
  4257. tmp |= (0x3F << 8);
  4258. intel_sbi_write(dev_priv, 0x203C, tmp, SBI_MPHY);
  4259. tmp = intel_sbi_read(dev_priv, 0x213C, SBI_MPHY);
  4260. tmp |= (0x3F << 8);
  4261. intel_sbi_write(dev_priv, 0x213C, tmp, SBI_MPHY);
  4262. }
  4263. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  4264. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4265. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  4266. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  4267. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  4268. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  4269. if (!is_sdv) {
  4270. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  4271. tmp &= ~(7 << 13);
  4272. tmp |= (5 << 13);
  4273. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  4274. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  4275. tmp &= ~(7 << 13);
  4276. tmp |= (5 << 13);
  4277. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  4278. }
  4279. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  4280. tmp &= ~0xFF;
  4281. tmp |= 0x1C;
  4282. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  4283. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  4284. tmp &= ~0xFF;
  4285. tmp |= 0x1C;
  4286. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  4287. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  4288. tmp &= ~(0xFF << 16);
  4289. tmp |= (0x1C << 16);
  4290. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  4291. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  4292. tmp &= ~(0xFF << 16);
  4293. tmp |= (0x1C << 16);
  4294. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  4295. if (!is_sdv) {
  4296. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  4297. tmp |= (1 << 27);
  4298. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  4299. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  4300. tmp |= (1 << 27);
  4301. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  4302. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  4303. tmp &= ~(0xF << 28);
  4304. tmp |= (4 << 28);
  4305. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  4306. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  4307. tmp &= ~(0xF << 28);
  4308. tmp |= (4 << 28);
  4309. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  4310. }
  4311. /* ULT uses SBI_GEN0, but ULT doesn't have VGA, so we don't care. */
  4312. tmp = intel_sbi_read(dev_priv, SBI_DBUFF0, SBI_ICLK);
  4313. tmp |= SBI_DBUFF0_ENABLE;
  4314. intel_sbi_write(dev_priv, SBI_DBUFF0, tmp, SBI_ICLK);
  4315. mutex_unlock(&dev_priv->dpio_lock);
  4316. }
  4317. /*
  4318. * Initialize reference clocks when the driver loads
  4319. */
  4320. void intel_init_pch_refclk(struct drm_device *dev)
  4321. {
  4322. if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
  4323. ironlake_init_pch_refclk(dev);
  4324. else if (HAS_PCH_LPT(dev))
  4325. lpt_init_pch_refclk(dev);
  4326. }
  4327. static int ironlake_get_refclk(struct drm_crtc *crtc)
  4328. {
  4329. struct drm_device *dev = crtc->dev;
  4330. struct drm_i915_private *dev_priv = dev->dev_private;
  4331. struct intel_encoder *encoder;
  4332. struct intel_encoder *edp_encoder = NULL;
  4333. int num_connectors = 0;
  4334. bool is_lvds = false;
  4335. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4336. switch (encoder->type) {
  4337. case INTEL_OUTPUT_LVDS:
  4338. is_lvds = true;
  4339. break;
  4340. case INTEL_OUTPUT_EDP:
  4341. edp_encoder = encoder;
  4342. break;
  4343. }
  4344. num_connectors++;
  4345. }
  4346. if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
  4347. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  4348. dev_priv->lvds_ssc_freq);
  4349. return dev_priv->lvds_ssc_freq * 1000;
  4350. }
  4351. return 120000;
  4352. }
  4353. static void ironlake_set_pipeconf(struct drm_crtc *crtc,
  4354. struct drm_display_mode *adjusted_mode,
  4355. bool dither)
  4356. {
  4357. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4358. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4359. int pipe = intel_crtc->pipe;
  4360. uint32_t val;
  4361. val = I915_READ(PIPECONF(pipe));
  4362. val &= ~PIPECONF_BPC_MASK;
  4363. switch (intel_crtc->config.pipe_bpp) {
  4364. case 18:
  4365. val |= PIPECONF_6BPC;
  4366. break;
  4367. case 24:
  4368. val |= PIPECONF_8BPC;
  4369. break;
  4370. case 30:
  4371. val |= PIPECONF_10BPC;
  4372. break;
  4373. case 36:
  4374. val |= PIPECONF_12BPC;
  4375. break;
  4376. default:
  4377. /* Case prevented by intel_choose_pipe_bpp_dither. */
  4378. BUG();
  4379. }
  4380. val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
  4381. if (dither)
  4382. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4383. val &= ~PIPECONF_INTERLACE_MASK;
  4384. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  4385. val |= PIPECONF_INTERLACED_ILK;
  4386. else
  4387. val |= PIPECONF_PROGRESSIVE;
  4388. if (intel_crtc->config.limited_color_range)
  4389. val |= PIPECONF_COLOR_RANGE_SELECT;
  4390. else
  4391. val &= ~PIPECONF_COLOR_RANGE_SELECT;
  4392. I915_WRITE(PIPECONF(pipe), val);
  4393. POSTING_READ(PIPECONF(pipe));
  4394. }
  4395. /*
  4396. * Set up the pipe CSC unit.
  4397. *
  4398. * Currently only full range RGB to limited range RGB conversion
  4399. * is supported, but eventually this should handle various
  4400. * RGB<->YCbCr scenarios as well.
  4401. */
  4402. static void intel_set_pipe_csc(struct drm_crtc *crtc)
  4403. {
  4404. struct drm_device *dev = crtc->dev;
  4405. struct drm_i915_private *dev_priv = dev->dev_private;
  4406. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4407. int pipe = intel_crtc->pipe;
  4408. uint16_t coeff = 0x7800; /* 1.0 */
  4409. /*
  4410. * TODO: Check what kind of values actually come out of the pipe
  4411. * with these coeff/postoff values and adjust to get the best
  4412. * accuracy. Perhaps we even need to take the bpc value into
  4413. * consideration.
  4414. */
  4415. if (intel_crtc->config.limited_color_range)
  4416. coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
  4417. /*
  4418. * GY/GU and RY/RU should be the other way around according
  4419. * to BSpec, but reality doesn't agree. Just set them up in
  4420. * a way that results in the correct picture.
  4421. */
  4422. I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
  4423. I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
  4424. I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
  4425. I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
  4426. I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
  4427. I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
  4428. I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
  4429. I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
  4430. I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
  4431. if (INTEL_INFO(dev)->gen > 6) {
  4432. uint16_t postoff = 0;
  4433. if (intel_crtc->config.limited_color_range)
  4434. postoff = (16 * (1 << 13) / 255) & 0x1fff;
  4435. I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
  4436. I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
  4437. I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
  4438. I915_WRITE(PIPE_CSC_MODE(pipe), 0);
  4439. } else {
  4440. uint32_t mode = CSC_MODE_YUV_TO_RGB;
  4441. if (intel_crtc->config.limited_color_range)
  4442. mode |= CSC_BLACK_SCREEN_OFFSET;
  4443. I915_WRITE(PIPE_CSC_MODE(pipe), mode);
  4444. }
  4445. }
  4446. static void haswell_set_pipeconf(struct drm_crtc *crtc,
  4447. struct drm_display_mode *adjusted_mode,
  4448. bool dither)
  4449. {
  4450. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  4451. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4452. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  4453. uint32_t val;
  4454. val = I915_READ(PIPECONF(cpu_transcoder));
  4455. val &= ~(PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_MASK);
  4456. if (dither)
  4457. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  4458. val &= ~PIPECONF_INTERLACE_MASK_HSW;
  4459. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  4460. val |= PIPECONF_INTERLACED_ILK;
  4461. else
  4462. val |= PIPECONF_PROGRESSIVE;
  4463. I915_WRITE(PIPECONF(cpu_transcoder), val);
  4464. POSTING_READ(PIPECONF(cpu_transcoder));
  4465. }
  4466. static bool ironlake_compute_clocks(struct drm_crtc *crtc,
  4467. struct drm_display_mode *adjusted_mode,
  4468. intel_clock_t *clock,
  4469. bool *has_reduced_clock,
  4470. intel_clock_t *reduced_clock)
  4471. {
  4472. struct drm_device *dev = crtc->dev;
  4473. struct drm_i915_private *dev_priv = dev->dev_private;
  4474. struct intel_encoder *intel_encoder;
  4475. int refclk;
  4476. const intel_limit_t *limit;
  4477. bool ret, is_sdvo = false, is_tv = false, is_lvds = false;
  4478. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4479. switch (intel_encoder->type) {
  4480. case INTEL_OUTPUT_LVDS:
  4481. is_lvds = true;
  4482. break;
  4483. case INTEL_OUTPUT_SDVO:
  4484. case INTEL_OUTPUT_HDMI:
  4485. is_sdvo = true;
  4486. if (intel_encoder->needs_tv_clock)
  4487. is_tv = true;
  4488. break;
  4489. case INTEL_OUTPUT_TVOUT:
  4490. is_tv = true;
  4491. break;
  4492. }
  4493. }
  4494. refclk = ironlake_get_refclk(crtc);
  4495. /*
  4496. * Returns a set of divisors for the desired target clock with the given
  4497. * refclk, or FALSE. The returned values represent the clock equation:
  4498. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  4499. */
  4500. limit = intel_limit(crtc, refclk);
  4501. ret = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, NULL,
  4502. clock);
  4503. if (!ret)
  4504. return false;
  4505. if (is_lvds && dev_priv->lvds_downclock_avail) {
  4506. /*
  4507. * Ensure we match the reduced clock's P to the target clock.
  4508. * If the clocks don't match, we can't switch the display clock
  4509. * by using the FP0/FP1. In such case we will disable the LVDS
  4510. * downclock feature.
  4511. */
  4512. *has_reduced_clock = limit->find_pll(limit, crtc,
  4513. dev_priv->lvds_downclock,
  4514. refclk,
  4515. clock,
  4516. reduced_clock);
  4517. }
  4518. if (is_sdvo && is_tv)
  4519. i9xx_adjust_sdvo_tv_clock(to_intel_crtc(crtc));
  4520. return true;
  4521. }
  4522. static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
  4523. {
  4524. struct drm_i915_private *dev_priv = dev->dev_private;
  4525. uint32_t temp;
  4526. temp = I915_READ(SOUTH_CHICKEN1);
  4527. if (temp & FDI_BC_BIFURCATION_SELECT)
  4528. return;
  4529. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  4530. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  4531. temp |= FDI_BC_BIFURCATION_SELECT;
  4532. DRM_DEBUG_KMS("enabling fdi C rx\n");
  4533. I915_WRITE(SOUTH_CHICKEN1, temp);
  4534. POSTING_READ(SOUTH_CHICKEN1);
  4535. }
  4536. static bool ironlake_check_fdi_lanes(struct intel_crtc *intel_crtc)
  4537. {
  4538. struct drm_device *dev = intel_crtc->base.dev;
  4539. struct drm_i915_private *dev_priv = dev->dev_private;
  4540. struct intel_crtc *pipe_B_crtc =
  4541. to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
  4542. DRM_DEBUG_KMS("checking fdi config on pipe %i, lanes %i\n",
  4543. intel_crtc->pipe, intel_crtc->fdi_lanes);
  4544. if (intel_crtc->fdi_lanes > 4) {
  4545. DRM_DEBUG_KMS("invalid fdi lane config on pipe %i: %i lanes\n",
  4546. intel_crtc->pipe, intel_crtc->fdi_lanes);
  4547. /* Clamp lanes to avoid programming the hw with bogus values. */
  4548. intel_crtc->fdi_lanes = 4;
  4549. return false;
  4550. }
  4551. if (INTEL_INFO(dev)->num_pipes == 2)
  4552. return true;
  4553. switch (intel_crtc->pipe) {
  4554. case PIPE_A:
  4555. return true;
  4556. case PIPE_B:
  4557. if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
  4558. intel_crtc->fdi_lanes > 2) {
  4559. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
  4560. intel_crtc->pipe, intel_crtc->fdi_lanes);
  4561. /* Clamp lanes to avoid programming the hw with bogus values. */
  4562. intel_crtc->fdi_lanes = 2;
  4563. return false;
  4564. }
  4565. if (intel_crtc->fdi_lanes > 2)
  4566. WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
  4567. else
  4568. cpt_enable_fdi_bc_bifurcation(dev);
  4569. return true;
  4570. case PIPE_C:
  4571. if (!pipe_B_crtc->base.enabled || pipe_B_crtc->fdi_lanes <= 2) {
  4572. if (intel_crtc->fdi_lanes > 2) {
  4573. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %i: %i lanes\n",
  4574. intel_crtc->pipe, intel_crtc->fdi_lanes);
  4575. /* Clamp lanes to avoid programming the hw with bogus values. */
  4576. intel_crtc->fdi_lanes = 2;
  4577. return false;
  4578. }
  4579. } else {
  4580. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  4581. return false;
  4582. }
  4583. cpt_enable_fdi_bc_bifurcation(dev);
  4584. return true;
  4585. default:
  4586. BUG();
  4587. }
  4588. }
  4589. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  4590. {
  4591. /*
  4592. * Account for spread spectrum to avoid
  4593. * oversubscribing the link. Max center spread
  4594. * is 2.5%; use 5% for safety's sake.
  4595. */
  4596. u32 bps = target_clock * bpp * 21 / 20;
  4597. return bps / (link_bw * 8) + 1;
  4598. }
  4599. void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  4600. struct intel_link_m_n *m_n)
  4601. {
  4602. struct drm_device *dev = crtc->base.dev;
  4603. struct drm_i915_private *dev_priv = dev->dev_private;
  4604. int pipe = crtc->pipe;
  4605. I915_WRITE(TRANSDATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4606. I915_WRITE(TRANSDATA_N1(pipe), m_n->gmch_n);
  4607. I915_WRITE(TRANSDPLINK_M1(pipe), m_n->link_m);
  4608. I915_WRITE(TRANSDPLINK_N1(pipe), m_n->link_n);
  4609. }
  4610. void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  4611. struct intel_link_m_n *m_n)
  4612. {
  4613. struct drm_device *dev = crtc->base.dev;
  4614. struct drm_i915_private *dev_priv = dev->dev_private;
  4615. int pipe = crtc->pipe;
  4616. enum transcoder transcoder = crtc->config.cpu_transcoder;
  4617. if (INTEL_INFO(dev)->gen >= 5) {
  4618. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4619. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  4620. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  4621. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  4622. } else {
  4623. I915_WRITE(PIPE_GMCH_DATA_M(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  4624. I915_WRITE(PIPE_GMCH_DATA_N(pipe), m_n->gmch_n);
  4625. I915_WRITE(PIPE_DP_LINK_M(pipe), m_n->link_m);
  4626. I915_WRITE(PIPE_DP_LINK_N(pipe), m_n->link_n);
  4627. }
  4628. }
  4629. static void ironlake_fdi_set_m_n(struct drm_crtc *crtc)
  4630. {
  4631. struct drm_device *dev = crtc->dev;
  4632. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4633. struct drm_display_mode *adjusted_mode =
  4634. &intel_crtc->config.adjusted_mode;
  4635. struct intel_link_m_n m_n = {0};
  4636. int target_clock, lane, link_bw;
  4637. /* FDI is a binary signal running at ~2.7GHz, encoding
  4638. * each output octet as 10 bits. The actual frequency
  4639. * is stored as a divider into a 100MHz clock, and the
  4640. * mode pixel clock is stored in units of 1KHz.
  4641. * Hence the bw of each lane in terms of the mode signal
  4642. * is:
  4643. */
  4644. link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
  4645. if (intel_crtc->config.pixel_target_clock)
  4646. target_clock = intel_crtc->config.pixel_target_clock;
  4647. else
  4648. target_clock = adjusted_mode->clock;
  4649. lane = ironlake_get_lanes_required(target_clock, link_bw,
  4650. intel_crtc->config.pipe_bpp);
  4651. intel_crtc->fdi_lanes = lane;
  4652. if (intel_crtc->config.pixel_multiplier > 1)
  4653. link_bw *= intel_crtc->config.pixel_multiplier;
  4654. intel_link_compute_m_n(intel_crtc->config.pipe_bpp, lane, target_clock,
  4655. link_bw, &m_n);
  4656. intel_cpu_transcoder_set_m_n(intel_crtc, &m_n);
  4657. }
  4658. static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  4659. intel_clock_t *clock, u32 *fp,
  4660. intel_clock_t *reduced_clock, u32 *fp2)
  4661. {
  4662. struct drm_crtc *crtc = &intel_crtc->base;
  4663. struct drm_device *dev = crtc->dev;
  4664. struct drm_i915_private *dev_priv = dev->dev_private;
  4665. struct intel_encoder *intel_encoder;
  4666. uint32_t dpll;
  4667. int factor, num_connectors = 0;
  4668. bool is_lvds = false, is_sdvo = false, is_tv = false;
  4669. for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
  4670. switch (intel_encoder->type) {
  4671. case INTEL_OUTPUT_LVDS:
  4672. is_lvds = true;
  4673. break;
  4674. case INTEL_OUTPUT_SDVO:
  4675. case INTEL_OUTPUT_HDMI:
  4676. is_sdvo = true;
  4677. if (intel_encoder->needs_tv_clock)
  4678. is_tv = true;
  4679. break;
  4680. case INTEL_OUTPUT_TVOUT:
  4681. is_tv = true;
  4682. break;
  4683. }
  4684. num_connectors++;
  4685. }
  4686. /* Enable autotuning of the PLL clock (if permissible) */
  4687. factor = 21;
  4688. if (is_lvds) {
  4689. if ((intel_panel_use_ssc(dev_priv) &&
  4690. dev_priv->lvds_ssc_freq == 100) ||
  4691. (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
  4692. factor = 25;
  4693. } else if (is_sdvo && is_tv)
  4694. factor = 20;
  4695. if (clock->m < factor * clock->n)
  4696. *fp |= FP_CB_TUNE;
  4697. if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
  4698. *fp2 |= FP_CB_TUNE;
  4699. dpll = 0;
  4700. if (is_lvds)
  4701. dpll |= DPLLB_MODE_LVDS;
  4702. else
  4703. dpll |= DPLLB_MODE_DAC_SERIAL;
  4704. if (is_sdvo) {
  4705. if (intel_crtc->config.pixel_multiplier > 1) {
  4706. dpll |= (intel_crtc->config.pixel_multiplier - 1)
  4707. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  4708. }
  4709. dpll |= DPLL_DVO_HIGH_SPEED;
  4710. }
  4711. if (intel_crtc->config.has_dp_encoder &&
  4712. intel_crtc->config.has_pch_encoder)
  4713. dpll |= DPLL_DVO_HIGH_SPEED;
  4714. /* compute bitmask from p1 value */
  4715. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  4716. /* also FPA1 */
  4717. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  4718. switch (clock->p2) {
  4719. case 5:
  4720. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  4721. break;
  4722. case 7:
  4723. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  4724. break;
  4725. case 10:
  4726. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  4727. break;
  4728. case 14:
  4729. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  4730. break;
  4731. }
  4732. if (is_sdvo && is_tv)
  4733. dpll |= PLL_REF_INPUT_TVCLKINBC;
  4734. else if (is_tv)
  4735. /* XXX: just matching BIOS for now */
  4736. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  4737. dpll |= 3;
  4738. else if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
  4739. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  4740. else
  4741. dpll |= PLL_REF_INPUT_DREFCLK;
  4742. return dpll;
  4743. }
  4744. static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
  4745. int x, int y,
  4746. struct drm_framebuffer *fb)
  4747. {
  4748. struct drm_device *dev = crtc->dev;
  4749. struct drm_i915_private *dev_priv = dev->dev_private;
  4750. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4751. struct drm_display_mode *adjusted_mode =
  4752. &intel_crtc->config.adjusted_mode;
  4753. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  4754. int pipe = intel_crtc->pipe;
  4755. int plane = intel_crtc->plane;
  4756. int num_connectors = 0;
  4757. intel_clock_t clock, reduced_clock;
  4758. u32 dpll, fp = 0, fp2 = 0;
  4759. bool ok, has_reduced_clock = false;
  4760. bool is_lvds = false;
  4761. struct intel_encoder *encoder;
  4762. int ret;
  4763. bool dither, fdi_config_ok;
  4764. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4765. switch (encoder->type) {
  4766. case INTEL_OUTPUT_LVDS:
  4767. is_lvds = true;
  4768. break;
  4769. }
  4770. num_connectors++;
  4771. }
  4772. WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
  4773. "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
  4774. intel_crtc->config.cpu_transcoder = pipe;
  4775. ok = ironlake_compute_clocks(crtc, adjusted_mode, &clock,
  4776. &has_reduced_clock, &reduced_clock);
  4777. if (!ok) {
  4778. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  4779. return -EINVAL;
  4780. }
  4781. /* Compat-code for transition, will disappear. */
  4782. if (!intel_crtc->config.clock_set) {
  4783. intel_crtc->config.dpll.n = clock.n;
  4784. intel_crtc->config.dpll.m1 = clock.m1;
  4785. intel_crtc->config.dpll.m2 = clock.m2;
  4786. intel_crtc->config.dpll.p1 = clock.p1;
  4787. intel_crtc->config.dpll.p2 = clock.p2;
  4788. }
  4789. /* Ensure that the cursor is valid for the new mode before changing... */
  4790. intel_crtc_update_cursor(crtc, true);
  4791. /* determine panel color depth */
  4792. dither = intel_crtc->config.dither;
  4793. if (is_lvds && dev_priv->lvds_dither)
  4794. dither = true;
  4795. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  4796. if (has_reduced_clock)
  4797. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  4798. reduced_clock.m2;
  4799. dpll = ironlake_compute_dpll(intel_crtc, &clock, &fp, &reduced_clock,
  4800. has_reduced_clock ? &fp2 : NULL);
  4801. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  4802. drm_mode_debug_printmodeline(mode);
  4803. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  4804. if (intel_crtc->config.has_pch_encoder) {
  4805. struct intel_pch_pll *pll;
  4806. pll = intel_get_pch_pll(intel_crtc, dpll, fp);
  4807. if (pll == NULL) {
  4808. DRM_DEBUG_DRIVER("failed to find PLL for pipe %d\n",
  4809. pipe);
  4810. return -EINVAL;
  4811. }
  4812. } else
  4813. intel_put_pch_pll(intel_crtc);
  4814. if (intel_crtc->config.has_dp_encoder)
  4815. intel_dp_set_m_n(intel_crtc);
  4816. for_each_encoder_on_crtc(dev, crtc, encoder)
  4817. if (encoder->pre_pll_enable)
  4818. encoder->pre_pll_enable(encoder);
  4819. if (intel_crtc->pch_pll) {
  4820. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4821. /* Wait for the clocks to stabilize. */
  4822. POSTING_READ(intel_crtc->pch_pll->pll_reg);
  4823. udelay(150);
  4824. /* The pixel multiplier can only be updated once the
  4825. * DPLL is enabled and the clocks are stable.
  4826. *
  4827. * So write it again.
  4828. */
  4829. I915_WRITE(intel_crtc->pch_pll->pll_reg, dpll);
  4830. }
  4831. intel_crtc->lowfreq_avail = false;
  4832. if (intel_crtc->pch_pll) {
  4833. if (is_lvds && has_reduced_clock && i915_powersave) {
  4834. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp2);
  4835. intel_crtc->lowfreq_avail = true;
  4836. } else {
  4837. I915_WRITE(intel_crtc->pch_pll->fp1_reg, fp);
  4838. }
  4839. }
  4840. intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
  4841. /* Note, this also computes intel_crtc->fdi_lanes which is used below in
  4842. * ironlake_check_fdi_lanes. */
  4843. intel_crtc->fdi_lanes = 0;
  4844. if (intel_crtc->config.has_pch_encoder)
  4845. ironlake_fdi_set_m_n(crtc);
  4846. fdi_config_ok = ironlake_check_fdi_lanes(intel_crtc);
  4847. ironlake_set_pipeconf(crtc, adjusted_mode, dither);
  4848. intel_wait_for_vblank(dev, pipe);
  4849. /* Set up the display plane register */
  4850. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
  4851. POSTING_READ(DSPCNTR(plane));
  4852. ret = intel_pipe_set_base(crtc, x, y, fb);
  4853. intel_update_watermarks(dev);
  4854. intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
  4855. return fdi_config_ok ? ret : -EINVAL;
  4856. }
  4857. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  4858. struct intel_crtc_config *pipe_config)
  4859. {
  4860. struct drm_device *dev = crtc->base.dev;
  4861. struct drm_i915_private *dev_priv = dev->dev_private;
  4862. uint32_t tmp;
  4863. tmp = I915_READ(PIPECONF(crtc->pipe));
  4864. if (!(tmp & PIPECONF_ENABLE))
  4865. return false;
  4866. if (I915_READ(TRANSCONF(crtc->pipe)) & TRANS_ENABLE)
  4867. pipe_config->has_pch_encoder = true;
  4868. return true;
  4869. }
  4870. static void haswell_modeset_global_resources(struct drm_device *dev)
  4871. {
  4872. struct drm_i915_private *dev_priv = dev->dev_private;
  4873. bool enable = false;
  4874. struct intel_crtc *crtc;
  4875. struct intel_encoder *encoder;
  4876. list_for_each_entry(crtc, &dev->mode_config.crtc_list, base.head) {
  4877. if (crtc->pipe != PIPE_A && crtc->base.enabled)
  4878. enable = true;
  4879. /* XXX: Should check for edp transcoder here, but thanks to init
  4880. * sequence that's not yet available. Just in case desktop eDP
  4881. * on PORT D is possible on haswell, too. */
  4882. }
  4883. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  4884. base.head) {
  4885. if (encoder->type != INTEL_OUTPUT_EDP &&
  4886. encoder->connectors_active)
  4887. enable = true;
  4888. }
  4889. /* Even the eDP panel fitter is outside the always-on well. */
  4890. if (dev_priv->pch_pf_size)
  4891. enable = true;
  4892. intel_set_power_well(dev, enable);
  4893. }
  4894. static int haswell_crtc_mode_set(struct drm_crtc *crtc,
  4895. int x, int y,
  4896. struct drm_framebuffer *fb)
  4897. {
  4898. struct drm_device *dev = crtc->dev;
  4899. struct drm_i915_private *dev_priv = dev->dev_private;
  4900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4901. struct drm_display_mode *adjusted_mode =
  4902. &intel_crtc->config.adjusted_mode;
  4903. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  4904. int pipe = intel_crtc->pipe;
  4905. int plane = intel_crtc->plane;
  4906. int num_connectors = 0;
  4907. bool is_cpu_edp = false;
  4908. struct intel_encoder *encoder;
  4909. int ret;
  4910. bool dither;
  4911. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4912. switch (encoder->type) {
  4913. case INTEL_OUTPUT_EDP:
  4914. if (!intel_encoder_is_pch_edp(&encoder->base))
  4915. is_cpu_edp = true;
  4916. break;
  4917. }
  4918. num_connectors++;
  4919. }
  4920. if (is_cpu_edp)
  4921. intel_crtc->config.cpu_transcoder = TRANSCODER_EDP;
  4922. else
  4923. intel_crtc->config.cpu_transcoder = pipe;
  4924. /* We are not sure yet this won't happen. */
  4925. WARN(!HAS_PCH_LPT(dev), "Unexpected PCH type %d\n",
  4926. INTEL_PCH_TYPE(dev));
  4927. WARN(num_connectors != 1, "%d connectors attached to pipe %c\n",
  4928. num_connectors, pipe_name(pipe));
  4929. WARN_ON(I915_READ(PIPECONF(intel_crtc->config.cpu_transcoder)) &
  4930. (PIPECONF_ENABLE | I965_PIPECONF_ACTIVE));
  4931. WARN_ON(I915_READ(DSPCNTR(plane)) & DISPLAY_PLANE_ENABLE);
  4932. if (!intel_ddi_pll_mode_set(crtc, adjusted_mode->clock))
  4933. return -EINVAL;
  4934. /* Ensure that the cursor is valid for the new mode before changing... */
  4935. intel_crtc_update_cursor(crtc, true);
  4936. /* determine panel color depth */
  4937. dither = intel_crtc->config.dither;
  4938. DRM_DEBUG_KMS("Mode for pipe %d:\n", pipe);
  4939. drm_mode_debug_printmodeline(mode);
  4940. if (intel_crtc->config.has_dp_encoder)
  4941. intel_dp_set_m_n(intel_crtc);
  4942. intel_crtc->lowfreq_avail = false;
  4943. intel_set_pipe_timings(intel_crtc, mode, adjusted_mode);
  4944. if (intel_crtc->config.has_pch_encoder)
  4945. ironlake_fdi_set_m_n(crtc);
  4946. haswell_set_pipeconf(crtc, adjusted_mode, dither);
  4947. intel_set_pipe_csc(crtc);
  4948. /* Set up the display plane register */
  4949. I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
  4950. POSTING_READ(DSPCNTR(plane));
  4951. ret = intel_pipe_set_base(crtc, x, y, fb);
  4952. intel_update_watermarks(dev);
  4953. intel_update_linetime_watermarks(dev, pipe, adjusted_mode);
  4954. return ret;
  4955. }
  4956. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  4957. struct intel_crtc_config *pipe_config)
  4958. {
  4959. struct drm_device *dev = crtc->base.dev;
  4960. struct drm_i915_private *dev_priv = dev->dev_private;
  4961. uint32_t tmp;
  4962. tmp = I915_READ(PIPECONF(crtc->config.cpu_transcoder));
  4963. if (!(tmp & PIPECONF_ENABLE))
  4964. return false;
  4965. /*
  4966. * aswell has only FDI/PCH transcoder A. It is which is connected to
  4967. * DDI E. So just check whether this pipe is wired to DDI E and whether
  4968. * the PCH transcoder is on.
  4969. */
  4970. tmp = I915_READ(TRANS_DDI_FUNC_CTL(crtc->pipe));
  4971. if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
  4972. I915_READ(TRANSCONF(PIPE_A)) & TRANS_ENABLE)
  4973. pipe_config->has_pch_encoder = true;
  4974. return true;
  4975. }
  4976. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  4977. int x, int y,
  4978. struct drm_framebuffer *fb)
  4979. {
  4980. struct drm_device *dev = crtc->dev;
  4981. struct drm_i915_private *dev_priv = dev->dev_private;
  4982. struct drm_encoder_helper_funcs *encoder_funcs;
  4983. struct intel_encoder *encoder;
  4984. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4985. struct drm_display_mode *adjusted_mode =
  4986. &intel_crtc->config.adjusted_mode;
  4987. struct drm_display_mode *mode = &intel_crtc->config.requested_mode;
  4988. int pipe = intel_crtc->pipe;
  4989. int ret;
  4990. drm_vblank_pre_modeset(dev, pipe);
  4991. ret = dev_priv->display.crtc_mode_set(crtc, x, y, fb);
  4992. drm_vblank_post_modeset(dev, pipe);
  4993. if (ret != 0)
  4994. return ret;
  4995. for_each_encoder_on_crtc(dev, crtc, encoder) {
  4996. DRM_DEBUG_KMS("[ENCODER:%d:%s] set [MODE:%d:%s]\n",
  4997. encoder->base.base.id,
  4998. drm_get_encoder_name(&encoder->base),
  4999. mode->base.id, mode->name);
  5000. if (encoder->mode_set) {
  5001. encoder->mode_set(encoder);
  5002. } else {
  5003. encoder_funcs = encoder->base.helper_private;
  5004. encoder_funcs->mode_set(&encoder->base, mode, adjusted_mode);
  5005. }
  5006. }
  5007. return 0;
  5008. }
  5009. static bool intel_eld_uptodate(struct drm_connector *connector,
  5010. int reg_eldv, uint32_t bits_eldv,
  5011. int reg_elda, uint32_t bits_elda,
  5012. int reg_edid)
  5013. {
  5014. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5015. uint8_t *eld = connector->eld;
  5016. uint32_t i;
  5017. i = I915_READ(reg_eldv);
  5018. i &= bits_eldv;
  5019. if (!eld[0])
  5020. return !i;
  5021. if (!i)
  5022. return false;
  5023. i = I915_READ(reg_elda);
  5024. i &= ~bits_elda;
  5025. I915_WRITE(reg_elda, i);
  5026. for (i = 0; i < eld[2]; i++)
  5027. if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
  5028. return false;
  5029. return true;
  5030. }
  5031. static void g4x_write_eld(struct drm_connector *connector,
  5032. struct drm_crtc *crtc)
  5033. {
  5034. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5035. uint8_t *eld = connector->eld;
  5036. uint32_t eldv;
  5037. uint32_t len;
  5038. uint32_t i;
  5039. i = I915_READ(G4X_AUD_VID_DID);
  5040. if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
  5041. eldv = G4X_ELDV_DEVCL_DEVBLC;
  5042. else
  5043. eldv = G4X_ELDV_DEVCTG;
  5044. if (intel_eld_uptodate(connector,
  5045. G4X_AUD_CNTL_ST, eldv,
  5046. G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
  5047. G4X_HDMIW_HDMIEDID))
  5048. return;
  5049. i = I915_READ(G4X_AUD_CNTL_ST);
  5050. i &= ~(eldv | G4X_ELD_ADDR);
  5051. len = (i >> 9) & 0x1f; /* ELD buffer size */
  5052. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5053. if (!eld[0])
  5054. return;
  5055. len = min_t(uint8_t, eld[2], len);
  5056. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5057. for (i = 0; i < len; i++)
  5058. I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
  5059. i = I915_READ(G4X_AUD_CNTL_ST);
  5060. i |= eldv;
  5061. I915_WRITE(G4X_AUD_CNTL_ST, i);
  5062. }
  5063. static void haswell_write_eld(struct drm_connector *connector,
  5064. struct drm_crtc *crtc)
  5065. {
  5066. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5067. uint8_t *eld = connector->eld;
  5068. struct drm_device *dev = crtc->dev;
  5069. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5070. uint32_t eldv;
  5071. uint32_t i;
  5072. int len;
  5073. int pipe = to_intel_crtc(crtc)->pipe;
  5074. int tmp;
  5075. int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
  5076. int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
  5077. int aud_config = HSW_AUD_CFG(pipe);
  5078. int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
  5079. DRM_DEBUG_DRIVER("HDMI: Haswell Audio initialize....\n");
  5080. /* Audio output enable */
  5081. DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
  5082. tmp = I915_READ(aud_cntrl_st2);
  5083. tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
  5084. I915_WRITE(aud_cntrl_st2, tmp);
  5085. /* Wait for 1 vertical blank */
  5086. intel_wait_for_vblank(dev, pipe);
  5087. /* Set ELD valid state */
  5088. tmp = I915_READ(aud_cntrl_st2);
  5089. DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%8x\n", tmp);
  5090. tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
  5091. I915_WRITE(aud_cntrl_st2, tmp);
  5092. tmp = I915_READ(aud_cntrl_st2);
  5093. DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%8x\n", tmp);
  5094. /* Enable HDMI mode */
  5095. tmp = I915_READ(aud_config);
  5096. DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%8x\n", tmp);
  5097. /* clear N_programing_enable and N_value_index */
  5098. tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
  5099. I915_WRITE(aud_config, tmp);
  5100. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5101. eldv = AUDIO_ELD_VALID_A << (pipe * 4);
  5102. intel_crtc->eld_vld = true;
  5103. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5104. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5105. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5106. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5107. } else
  5108. I915_WRITE(aud_config, 0);
  5109. if (intel_eld_uptodate(connector,
  5110. aud_cntrl_st2, eldv,
  5111. aud_cntl_st, IBX_ELD_ADDRESS,
  5112. hdmiw_hdmiedid))
  5113. return;
  5114. i = I915_READ(aud_cntrl_st2);
  5115. i &= ~eldv;
  5116. I915_WRITE(aud_cntrl_st2, i);
  5117. if (!eld[0])
  5118. return;
  5119. i = I915_READ(aud_cntl_st);
  5120. i &= ~IBX_ELD_ADDRESS;
  5121. I915_WRITE(aud_cntl_st, i);
  5122. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5123. DRM_DEBUG_DRIVER("port num:%d\n", i);
  5124. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5125. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5126. for (i = 0; i < len; i++)
  5127. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5128. i = I915_READ(aud_cntrl_st2);
  5129. i |= eldv;
  5130. I915_WRITE(aud_cntrl_st2, i);
  5131. }
  5132. static void ironlake_write_eld(struct drm_connector *connector,
  5133. struct drm_crtc *crtc)
  5134. {
  5135. struct drm_i915_private *dev_priv = connector->dev->dev_private;
  5136. uint8_t *eld = connector->eld;
  5137. uint32_t eldv;
  5138. uint32_t i;
  5139. int len;
  5140. int hdmiw_hdmiedid;
  5141. int aud_config;
  5142. int aud_cntl_st;
  5143. int aud_cntrl_st2;
  5144. int pipe = to_intel_crtc(crtc)->pipe;
  5145. if (HAS_PCH_IBX(connector->dev)) {
  5146. hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
  5147. aud_config = IBX_AUD_CFG(pipe);
  5148. aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
  5149. aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
  5150. } else {
  5151. hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
  5152. aud_config = CPT_AUD_CFG(pipe);
  5153. aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
  5154. aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
  5155. }
  5156. DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
  5157. i = I915_READ(aud_cntl_st);
  5158. i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
  5159. if (!i) {
  5160. DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
  5161. /* operate blindly on all ports */
  5162. eldv = IBX_ELD_VALIDB;
  5163. eldv |= IBX_ELD_VALIDB << 4;
  5164. eldv |= IBX_ELD_VALIDB << 8;
  5165. } else {
  5166. DRM_DEBUG_DRIVER("ELD on port %c\n", 'A' + i);
  5167. eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
  5168. }
  5169. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  5170. DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
  5171. eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
  5172. I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
  5173. } else
  5174. I915_WRITE(aud_config, 0);
  5175. if (intel_eld_uptodate(connector,
  5176. aud_cntrl_st2, eldv,
  5177. aud_cntl_st, IBX_ELD_ADDRESS,
  5178. hdmiw_hdmiedid))
  5179. return;
  5180. i = I915_READ(aud_cntrl_st2);
  5181. i &= ~eldv;
  5182. I915_WRITE(aud_cntrl_st2, i);
  5183. if (!eld[0])
  5184. return;
  5185. i = I915_READ(aud_cntl_st);
  5186. i &= ~IBX_ELD_ADDRESS;
  5187. I915_WRITE(aud_cntl_st, i);
  5188. len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
  5189. DRM_DEBUG_DRIVER("ELD size %d\n", len);
  5190. for (i = 0; i < len; i++)
  5191. I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
  5192. i = I915_READ(aud_cntrl_st2);
  5193. i |= eldv;
  5194. I915_WRITE(aud_cntrl_st2, i);
  5195. }
  5196. void intel_write_eld(struct drm_encoder *encoder,
  5197. struct drm_display_mode *mode)
  5198. {
  5199. struct drm_crtc *crtc = encoder->crtc;
  5200. struct drm_connector *connector;
  5201. struct drm_device *dev = encoder->dev;
  5202. struct drm_i915_private *dev_priv = dev->dev_private;
  5203. connector = drm_select_eld(encoder, mode);
  5204. if (!connector)
  5205. return;
  5206. DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5207. connector->base.id,
  5208. drm_get_connector_name(connector),
  5209. connector->encoder->base.id,
  5210. drm_get_encoder_name(connector->encoder));
  5211. connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
  5212. if (dev_priv->display.write_eld)
  5213. dev_priv->display.write_eld(connector, crtc);
  5214. }
  5215. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  5216. void intel_crtc_load_lut(struct drm_crtc *crtc)
  5217. {
  5218. struct drm_device *dev = crtc->dev;
  5219. struct drm_i915_private *dev_priv = dev->dev_private;
  5220. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5221. int palreg = PALETTE(intel_crtc->pipe);
  5222. int i;
  5223. /* The clocks have to be on to load the palette. */
  5224. if (!crtc->enabled || !intel_crtc->active)
  5225. return;
  5226. /* use legacy palette for Ironlake */
  5227. if (HAS_PCH_SPLIT(dev))
  5228. palreg = LGC_PALETTE(intel_crtc->pipe);
  5229. for (i = 0; i < 256; i++) {
  5230. I915_WRITE(palreg + 4 * i,
  5231. (intel_crtc->lut_r[i] << 16) |
  5232. (intel_crtc->lut_g[i] << 8) |
  5233. intel_crtc->lut_b[i]);
  5234. }
  5235. }
  5236. static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
  5237. {
  5238. struct drm_device *dev = crtc->dev;
  5239. struct drm_i915_private *dev_priv = dev->dev_private;
  5240. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5241. bool visible = base != 0;
  5242. u32 cntl;
  5243. if (intel_crtc->cursor_visible == visible)
  5244. return;
  5245. cntl = I915_READ(_CURACNTR);
  5246. if (visible) {
  5247. /* On these chipsets we can only modify the base whilst
  5248. * the cursor is disabled.
  5249. */
  5250. I915_WRITE(_CURABASE, base);
  5251. cntl &= ~(CURSOR_FORMAT_MASK);
  5252. /* XXX width must be 64, stride 256 => 0x00 << 28 */
  5253. cntl |= CURSOR_ENABLE |
  5254. CURSOR_GAMMA_ENABLE |
  5255. CURSOR_FORMAT_ARGB;
  5256. } else
  5257. cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  5258. I915_WRITE(_CURACNTR, cntl);
  5259. intel_crtc->cursor_visible = visible;
  5260. }
  5261. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
  5262. {
  5263. struct drm_device *dev = crtc->dev;
  5264. struct drm_i915_private *dev_priv = dev->dev_private;
  5265. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5266. int pipe = intel_crtc->pipe;
  5267. bool visible = base != 0;
  5268. if (intel_crtc->cursor_visible != visible) {
  5269. uint32_t cntl = I915_READ(CURCNTR(pipe));
  5270. if (base) {
  5271. cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  5272. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5273. cntl |= pipe << 28; /* Connect to correct pipe */
  5274. } else {
  5275. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5276. cntl |= CURSOR_MODE_DISABLE;
  5277. }
  5278. I915_WRITE(CURCNTR(pipe), cntl);
  5279. intel_crtc->cursor_visible = visible;
  5280. }
  5281. /* and commit changes on next vblank */
  5282. I915_WRITE(CURBASE(pipe), base);
  5283. }
  5284. static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
  5285. {
  5286. struct drm_device *dev = crtc->dev;
  5287. struct drm_i915_private *dev_priv = dev->dev_private;
  5288. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5289. int pipe = intel_crtc->pipe;
  5290. bool visible = base != 0;
  5291. if (intel_crtc->cursor_visible != visible) {
  5292. uint32_t cntl = I915_READ(CURCNTR_IVB(pipe));
  5293. if (base) {
  5294. cntl &= ~CURSOR_MODE;
  5295. cntl |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  5296. } else {
  5297. cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  5298. cntl |= CURSOR_MODE_DISABLE;
  5299. }
  5300. if (IS_HASWELL(dev))
  5301. cntl |= CURSOR_PIPE_CSC_ENABLE;
  5302. I915_WRITE(CURCNTR_IVB(pipe), cntl);
  5303. intel_crtc->cursor_visible = visible;
  5304. }
  5305. /* and commit changes on next vblank */
  5306. I915_WRITE(CURBASE_IVB(pipe), base);
  5307. }
  5308. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  5309. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  5310. bool on)
  5311. {
  5312. struct drm_device *dev = crtc->dev;
  5313. struct drm_i915_private *dev_priv = dev->dev_private;
  5314. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5315. int pipe = intel_crtc->pipe;
  5316. int x = intel_crtc->cursor_x;
  5317. int y = intel_crtc->cursor_y;
  5318. u32 base, pos;
  5319. bool visible;
  5320. pos = 0;
  5321. if (on && crtc->enabled && crtc->fb) {
  5322. base = intel_crtc->cursor_addr;
  5323. if (x > (int) crtc->fb->width)
  5324. base = 0;
  5325. if (y > (int) crtc->fb->height)
  5326. base = 0;
  5327. } else
  5328. base = 0;
  5329. if (x < 0) {
  5330. if (x + intel_crtc->cursor_width < 0)
  5331. base = 0;
  5332. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  5333. x = -x;
  5334. }
  5335. pos |= x << CURSOR_X_SHIFT;
  5336. if (y < 0) {
  5337. if (y + intel_crtc->cursor_height < 0)
  5338. base = 0;
  5339. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  5340. y = -y;
  5341. }
  5342. pos |= y << CURSOR_Y_SHIFT;
  5343. visible = base != 0;
  5344. if (!visible && !intel_crtc->cursor_visible)
  5345. return;
  5346. if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
  5347. I915_WRITE(CURPOS_IVB(pipe), pos);
  5348. ivb_update_cursor(crtc, base);
  5349. } else {
  5350. I915_WRITE(CURPOS(pipe), pos);
  5351. if (IS_845G(dev) || IS_I865G(dev))
  5352. i845_update_cursor(crtc, base);
  5353. else
  5354. i9xx_update_cursor(crtc, base);
  5355. }
  5356. }
  5357. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  5358. struct drm_file *file,
  5359. uint32_t handle,
  5360. uint32_t width, uint32_t height)
  5361. {
  5362. struct drm_device *dev = crtc->dev;
  5363. struct drm_i915_private *dev_priv = dev->dev_private;
  5364. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5365. struct drm_i915_gem_object *obj;
  5366. uint32_t addr;
  5367. int ret;
  5368. /* if we want to turn off the cursor ignore width and height */
  5369. if (!handle) {
  5370. DRM_DEBUG_KMS("cursor off\n");
  5371. addr = 0;
  5372. obj = NULL;
  5373. mutex_lock(&dev->struct_mutex);
  5374. goto finish;
  5375. }
  5376. /* Currently we only support 64x64 cursors */
  5377. if (width != 64 || height != 64) {
  5378. DRM_ERROR("we currently only support 64x64 cursors\n");
  5379. return -EINVAL;
  5380. }
  5381. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  5382. if (&obj->base == NULL)
  5383. return -ENOENT;
  5384. if (obj->base.size < width * height * 4) {
  5385. DRM_ERROR("buffer is to small\n");
  5386. ret = -ENOMEM;
  5387. goto fail;
  5388. }
  5389. /* we only need to pin inside GTT if cursor is non-phy */
  5390. mutex_lock(&dev->struct_mutex);
  5391. if (!dev_priv->info->cursor_needs_physical) {
  5392. unsigned alignment;
  5393. if (obj->tiling_mode) {
  5394. DRM_ERROR("cursor cannot be tiled\n");
  5395. ret = -EINVAL;
  5396. goto fail_locked;
  5397. }
  5398. /* Note that the w/a also requires 2 PTE of padding following
  5399. * the bo. We currently fill all unused PTE with the shadow
  5400. * page and so we should always have valid PTE following the
  5401. * cursor preventing the VT-d warning.
  5402. */
  5403. alignment = 0;
  5404. if (need_vtd_wa(dev))
  5405. alignment = 64*1024;
  5406. ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
  5407. if (ret) {
  5408. DRM_ERROR("failed to move cursor bo into the GTT\n");
  5409. goto fail_locked;
  5410. }
  5411. ret = i915_gem_object_put_fence(obj);
  5412. if (ret) {
  5413. DRM_ERROR("failed to release fence for cursor");
  5414. goto fail_unpin;
  5415. }
  5416. addr = obj->gtt_offset;
  5417. } else {
  5418. int align = IS_I830(dev) ? 16 * 1024 : 256;
  5419. ret = i915_gem_attach_phys_object(dev, obj,
  5420. (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
  5421. align);
  5422. if (ret) {
  5423. DRM_ERROR("failed to attach phys object\n");
  5424. goto fail_locked;
  5425. }
  5426. addr = obj->phys_obj->handle->busaddr;
  5427. }
  5428. if (IS_GEN2(dev))
  5429. I915_WRITE(CURSIZE, (height << 12) | width);
  5430. finish:
  5431. if (intel_crtc->cursor_bo) {
  5432. if (dev_priv->info->cursor_needs_physical) {
  5433. if (intel_crtc->cursor_bo != obj)
  5434. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  5435. } else
  5436. i915_gem_object_unpin(intel_crtc->cursor_bo);
  5437. drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
  5438. }
  5439. mutex_unlock(&dev->struct_mutex);
  5440. intel_crtc->cursor_addr = addr;
  5441. intel_crtc->cursor_bo = obj;
  5442. intel_crtc->cursor_width = width;
  5443. intel_crtc->cursor_height = height;
  5444. intel_crtc_update_cursor(crtc, true);
  5445. return 0;
  5446. fail_unpin:
  5447. i915_gem_object_unpin(obj);
  5448. fail_locked:
  5449. mutex_unlock(&dev->struct_mutex);
  5450. fail:
  5451. drm_gem_object_unreference_unlocked(&obj->base);
  5452. return ret;
  5453. }
  5454. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  5455. {
  5456. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5457. intel_crtc->cursor_x = x;
  5458. intel_crtc->cursor_y = y;
  5459. intel_crtc_update_cursor(crtc, true);
  5460. return 0;
  5461. }
  5462. /** Sets the color ramps on behalf of RandR */
  5463. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  5464. u16 blue, int regno)
  5465. {
  5466. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5467. intel_crtc->lut_r[regno] = red >> 8;
  5468. intel_crtc->lut_g[regno] = green >> 8;
  5469. intel_crtc->lut_b[regno] = blue >> 8;
  5470. }
  5471. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  5472. u16 *blue, int regno)
  5473. {
  5474. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5475. *red = intel_crtc->lut_r[regno] << 8;
  5476. *green = intel_crtc->lut_g[regno] << 8;
  5477. *blue = intel_crtc->lut_b[regno] << 8;
  5478. }
  5479. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  5480. u16 *blue, uint32_t start, uint32_t size)
  5481. {
  5482. int end = (start + size > 256) ? 256 : start + size, i;
  5483. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5484. for (i = start; i < end; i++) {
  5485. intel_crtc->lut_r[i] = red[i] >> 8;
  5486. intel_crtc->lut_g[i] = green[i] >> 8;
  5487. intel_crtc->lut_b[i] = blue[i] >> 8;
  5488. }
  5489. intel_crtc_load_lut(crtc);
  5490. }
  5491. /* VESA 640x480x72Hz mode to set on the pipe */
  5492. static struct drm_display_mode load_detect_mode = {
  5493. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  5494. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  5495. };
  5496. static struct drm_framebuffer *
  5497. intel_framebuffer_create(struct drm_device *dev,
  5498. struct drm_mode_fb_cmd2 *mode_cmd,
  5499. struct drm_i915_gem_object *obj)
  5500. {
  5501. struct intel_framebuffer *intel_fb;
  5502. int ret;
  5503. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  5504. if (!intel_fb) {
  5505. drm_gem_object_unreference_unlocked(&obj->base);
  5506. return ERR_PTR(-ENOMEM);
  5507. }
  5508. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  5509. if (ret) {
  5510. drm_gem_object_unreference_unlocked(&obj->base);
  5511. kfree(intel_fb);
  5512. return ERR_PTR(ret);
  5513. }
  5514. return &intel_fb->base;
  5515. }
  5516. static u32
  5517. intel_framebuffer_pitch_for_width(int width, int bpp)
  5518. {
  5519. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  5520. return ALIGN(pitch, 64);
  5521. }
  5522. static u32
  5523. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  5524. {
  5525. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  5526. return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
  5527. }
  5528. static struct drm_framebuffer *
  5529. intel_framebuffer_create_for_mode(struct drm_device *dev,
  5530. struct drm_display_mode *mode,
  5531. int depth, int bpp)
  5532. {
  5533. struct drm_i915_gem_object *obj;
  5534. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  5535. obj = i915_gem_alloc_object(dev,
  5536. intel_framebuffer_size_for_mode(mode, bpp));
  5537. if (obj == NULL)
  5538. return ERR_PTR(-ENOMEM);
  5539. mode_cmd.width = mode->hdisplay;
  5540. mode_cmd.height = mode->vdisplay;
  5541. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  5542. bpp);
  5543. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  5544. return intel_framebuffer_create(dev, &mode_cmd, obj);
  5545. }
  5546. static struct drm_framebuffer *
  5547. mode_fits_in_fbdev(struct drm_device *dev,
  5548. struct drm_display_mode *mode)
  5549. {
  5550. struct drm_i915_private *dev_priv = dev->dev_private;
  5551. struct drm_i915_gem_object *obj;
  5552. struct drm_framebuffer *fb;
  5553. if (dev_priv->fbdev == NULL)
  5554. return NULL;
  5555. obj = dev_priv->fbdev->ifb.obj;
  5556. if (obj == NULL)
  5557. return NULL;
  5558. fb = &dev_priv->fbdev->ifb.base;
  5559. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  5560. fb->bits_per_pixel))
  5561. return NULL;
  5562. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  5563. return NULL;
  5564. return fb;
  5565. }
  5566. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  5567. struct drm_display_mode *mode,
  5568. struct intel_load_detect_pipe *old)
  5569. {
  5570. struct intel_crtc *intel_crtc;
  5571. struct intel_encoder *intel_encoder =
  5572. intel_attached_encoder(connector);
  5573. struct drm_crtc *possible_crtc;
  5574. struct drm_encoder *encoder = &intel_encoder->base;
  5575. struct drm_crtc *crtc = NULL;
  5576. struct drm_device *dev = encoder->dev;
  5577. struct drm_framebuffer *fb;
  5578. int i = -1;
  5579. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5580. connector->base.id, drm_get_connector_name(connector),
  5581. encoder->base.id, drm_get_encoder_name(encoder));
  5582. /*
  5583. * Algorithm gets a little messy:
  5584. *
  5585. * - if the connector already has an assigned crtc, use it (but make
  5586. * sure it's on first)
  5587. *
  5588. * - try to find the first unused crtc that can drive this connector,
  5589. * and use that if we find one
  5590. */
  5591. /* See if we already have a CRTC for this connector */
  5592. if (encoder->crtc) {
  5593. crtc = encoder->crtc;
  5594. mutex_lock(&crtc->mutex);
  5595. old->dpms_mode = connector->dpms;
  5596. old->load_detect_temp = false;
  5597. /* Make sure the crtc and connector are running */
  5598. if (connector->dpms != DRM_MODE_DPMS_ON)
  5599. connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
  5600. return true;
  5601. }
  5602. /* Find an unused one (if possible) */
  5603. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  5604. i++;
  5605. if (!(encoder->possible_crtcs & (1 << i)))
  5606. continue;
  5607. if (!possible_crtc->enabled) {
  5608. crtc = possible_crtc;
  5609. break;
  5610. }
  5611. }
  5612. /*
  5613. * If we didn't find an unused CRTC, don't use any.
  5614. */
  5615. if (!crtc) {
  5616. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  5617. return false;
  5618. }
  5619. mutex_lock(&crtc->mutex);
  5620. intel_encoder->new_crtc = to_intel_crtc(crtc);
  5621. to_intel_connector(connector)->new_encoder = intel_encoder;
  5622. intel_crtc = to_intel_crtc(crtc);
  5623. old->dpms_mode = connector->dpms;
  5624. old->load_detect_temp = true;
  5625. old->release_fb = NULL;
  5626. if (!mode)
  5627. mode = &load_detect_mode;
  5628. /* We need a framebuffer large enough to accommodate all accesses
  5629. * that the plane may generate whilst we perform load detection.
  5630. * We can not rely on the fbcon either being present (we get called
  5631. * during its initialisation to detect all boot displays, or it may
  5632. * not even exist) or that it is large enough to satisfy the
  5633. * requested mode.
  5634. */
  5635. fb = mode_fits_in_fbdev(dev, mode);
  5636. if (fb == NULL) {
  5637. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  5638. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  5639. old->release_fb = fb;
  5640. } else
  5641. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  5642. if (IS_ERR(fb)) {
  5643. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  5644. mutex_unlock(&crtc->mutex);
  5645. return false;
  5646. }
  5647. if (intel_set_mode(crtc, mode, 0, 0, fb)) {
  5648. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  5649. if (old->release_fb)
  5650. old->release_fb->funcs->destroy(old->release_fb);
  5651. mutex_unlock(&crtc->mutex);
  5652. return false;
  5653. }
  5654. /* let the connector get through one full cycle before testing */
  5655. intel_wait_for_vblank(dev, intel_crtc->pipe);
  5656. return true;
  5657. }
  5658. void intel_release_load_detect_pipe(struct drm_connector *connector,
  5659. struct intel_load_detect_pipe *old)
  5660. {
  5661. struct intel_encoder *intel_encoder =
  5662. intel_attached_encoder(connector);
  5663. struct drm_encoder *encoder = &intel_encoder->base;
  5664. struct drm_crtc *crtc = encoder->crtc;
  5665. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  5666. connector->base.id, drm_get_connector_name(connector),
  5667. encoder->base.id, drm_get_encoder_name(encoder));
  5668. if (old->load_detect_temp) {
  5669. to_intel_connector(connector)->new_encoder = NULL;
  5670. intel_encoder->new_crtc = NULL;
  5671. intel_set_mode(crtc, NULL, 0, 0, NULL);
  5672. if (old->release_fb) {
  5673. drm_framebuffer_unregister_private(old->release_fb);
  5674. drm_framebuffer_unreference(old->release_fb);
  5675. }
  5676. mutex_unlock(&crtc->mutex);
  5677. return;
  5678. }
  5679. /* Switch crtc and encoder back off if necessary */
  5680. if (old->dpms_mode != DRM_MODE_DPMS_ON)
  5681. connector->funcs->dpms(connector, old->dpms_mode);
  5682. mutex_unlock(&crtc->mutex);
  5683. }
  5684. /* Returns the clock of the currently programmed mode of the given pipe. */
  5685. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  5686. {
  5687. struct drm_i915_private *dev_priv = dev->dev_private;
  5688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5689. int pipe = intel_crtc->pipe;
  5690. u32 dpll = I915_READ(DPLL(pipe));
  5691. u32 fp;
  5692. intel_clock_t clock;
  5693. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  5694. fp = I915_READ(FP0(pipe));
  5695. else
  5696. fp = I915_READ(FP1(pipe));
  5697. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  5698. if (IS_PINEVIEW(dev)) {
  5699. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  5700. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5701. } else {
  5702. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  5703. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  5704. }
  5705. if (!IS_GEN2(dev)) {
  5706. if (IS_PINEVIEW(dev))
  5707. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  5708. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  5709. else
  5710. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  5711. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5712. switch (dpll & DPLL_MODE_MASK) {
  5713. case DPLLB_MODE_DAC_SERIAL:
  5714. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  5715. 5 : 10;
  5716. break;
  5717. case DPLLB_MODE_LVDS:
  5718. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  5719. 7 : 14;
  5720. break;
  5721. default:
  5722. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  5723. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  5724. return 0;
  5725. }
  5726. /* XXX: Handle the 100Mhz refclk */
  5727. intel_clock(dev, 96000, &clock);
  5728. } else {
  5729. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  5730. if (is_lvds) {
  5731. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  5732. DPLL_FPA01_P1_POST_DIV_SHIFT);
  5733. clock.p2 = 14;
  5734. if ((dpll & PLL_REF_INPUT_MASK) ==
  5735. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  5736. /* XXX: might not be 66MHz */
  5737. intel_clock(dev, 66000, &clock);
  5738. } else
  5739. intel_clock(dev, 48000, &clock);
  5740. } else {
  5741. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  5742. clock.p1 = 2;
  5743. else {
  5744. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  5745. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  5746. }
  5747. if (dpll & PLL_P2_DIVIDE_BY_4)
  5748. clock.p2 = 4;
  5749. else
  5750. clock.p2 = 2;
  5751. intel_clock(dev, 48000, &clock);
  5752. }
  5753. }
  5754. /* XXX: It would be nice to validate the clocks, but we can't reuse
  5755. * i830PllIsValid() because it relies on the xf86_config connector
  5756. * configuration being accurate, which it isn't necessarily.
  5757. */
  5758. return clock.dot;
  5759. }
  5760. /** Returns the currently programmed mode of the given pipe. */
  5761. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  5762. struct drm_crtc *crtc)
  5763. {
  5764. struct drm_i915_private *dev_priv = dev->dev_private;
  5765. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5766. enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
  5767. struct drm_display_mode *mode;
  5768. int htot = I915_READ(HTOTAL(cpu_transcoder));
  5769. int hsync = I915_READ(HSYNC(cpu_transcoder));
  5770. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  5771. int vsync = I915_READ(VSYNC(cpu_transcoder));
  5772. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  5773. if (!mode)
  5774. return NULL;
  5775. mode->clock = intel_crtc_clock_get(dev, crtc);
  5776. mode->hdisplay = (htot & 0xffff) + 1;
  5777. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  5778. mode->hsync_start = (hsync & 0xffff) + 1;
  5779. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  5780. mode->vdisplay = (vtot & 0xffff) + 1;
  5781. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  5782. mode->vsync_start = (vsync & 0xffff) + 1;
  5783. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  5784. drm_mode_set_name(mode);
  5785. return mode;
  5786. }
  5787. static void intel_increase_pllclock(struct drm_crtc *crtc)
  5788. {
  5789. struct drm_device *dev = crtc->dev;
  5790. drm_i915_private_t *dev_priv = dev->dev_private;
  5791. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5792. int pipe = intel_crtc->pipe;
  5793. int dpll_reg = DPLL(pipe);
  5794. int dpll;
  5795. if (HAS_PCH_SPLIT(dev))
  5796. return;
  5797. if (!dev_priv->lvds_downclock_avail)
  5798. return;
  5799. dpll = I915_READ(dpll_reg);
  5800. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  5801. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  5802. assert_panel_unlocked(dev_priv, pipe);
  5803. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  5804. I915_WRITE(dpll_reg, dpll);
  5805. intel_wait_for_vblank(dev, pipe);
  5806. dpll = I915_READ(dpll_reg);
  5807. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  5808. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  5809. }
  5810. }
  5811. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  5812. {
  5813. struct drm_device *dev = crtc->dev;
  5814. drm_i915_private_t *dev_priv = dev->dev_private;
  5815. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5816. if (HAS_PCH_SPLIT(dev))
  5817. return;
  5818. if (!dev_priv->lvds_downclock_avail)
  5819. return;
  5820. /*
  5821. * Since this is called by a timer, we should never get here in
  5822. * the manual case.
  5823. */
  5824. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  5825. int pipe = intel_crtc->pipe;
  5826. int dpll_reg = DPLL(pipe);
  5827. int dpll;
  5828. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  5829. assert_panel_unlocked(dev_priv, pipe);
  5830. dpll = I915_READ(dpll_reg);
  5831. dpll |= DISPLAY_RATE_SELECT_FPA1;
  5832. I915_WRITE(dpll_reg, dpll);
  5833. intel_wait_for_vblank(dev, pipe);
  5834. dpll = I915_READ(dpll_reg);
  5835. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  5836. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  5837. }
  5838. }
  5839. void intel_mark_busy(struct drm_device *dev)
  5840. {
  5841. i915_update_gfx_val(dev->dev_private);
  5842. }
  5843. void intel_mark_idle(struct drm_device *dev)
  5844. {
  5845. struct drm_crtc *crtc;
  5846. if (!i915_powersave)
  5847. return;
  5848. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5849. if (!crtc->fb)
  5850. continue;
  5851. intel_decrease_pllclock(crtc);
  5852. }
  5853. }
  5854. void intel_mark_fb_busy(struct drm_i915_gem_object *obj)
  5855. {
  5856. struct drm_device *dev = obj->base.dev;
  5857. struct drm_crtc *crtc;
  5858. if (!i915_powersave)
  5859. return;
  5860. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  5861. if (!crtc->fb)
  5862. continue;
  5863. if (to_intel_framebuffer(crtc->fb)->obj == obj)
  5864. intel_increase_pllclock(crtc);
  5865. }
  5866. }
  5867. static void intel_crtc_destroy(struct drm_crtc *crtc)
  5868. {
  5869. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5870. struct drm_device *dev = crtc->dev;
  5871. struct intel_unpin_work *work;
  5872. unsigned long flags;
  5873. spin_lock_irqsave(&dev->event_lock, flags);
  5874. work = intel_crtc->unpin_work;
  5875. intel_crtc->unpin_work = NULL;
  5876. spin_unlock_irqrestore(&dev->event_lock, flags);
  5877. if (work) {
  5878. cancel_work_sync(&work->work);
  5879. kfree(work);
  5880. }
  5881. drm_crtc_cleanup(crtc);
  5882. kfree(intel_crtc);
  5883. }
  5884. static void intel_unpin_work_fn(struct work_struct *__work)
  5885. {
  5886. struct intel_unpin_work *work =
  5887. container_of(__work, struct intel_unpin_work, work);
  5888. struct drm_device *dev = work->crtc->dev;
  5889. mutex_lock(&dev->struct_mutex);
  5890. intel_unpin_fb_obj(work->old_fb_obj);
  5891. drm_gem_object_unreference(&work->pending_flip_obj->base);
  5892. drm_gem_object_unreference(&work->old_fb_obj->base);
  5893. intel_update_fbc(dev);
  5894. mutex_unlock(&dev->struct_mutex);
  5895. BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
  5896. atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
  5897. kfree(work);
  5898. }
  5899. static void do_intel_finish_page_flip(struct drm_device *dev,
  5900. struct drm_crtc *crtc)
  5901. {
  5902. drm_i915_private_t *dev_priv = dev->dev_private;
  5903. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5904. struct intel_unpin_work *work;
  5905. unsigned long flags;
  5906. /* Ignore early vblank irqs */
  5907. if (intel_crtc == NULL)
  5908. return;
  5909. spin_lock_irqsave(&dev->event_lock, flags);
  5910. work = intel_crtc->unpin_work;
  5911. /* Ensure we don't miss a work->pending update ... */
  5912. smp_rmb();
  5913. if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
  5914. spin_unlock_irqrestore(&dev->event_lock, flags);
  5915. return;
  5916. }
  5917. /* and that the unpin work is consistent wrt ->pending. */
  5918. smp_rmb();
  5919. intel_crtc->unpin_work = NULL;
  5920. if (work->event)
  5921. drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
  5922. drm_vblank_put(dev, intel_crtc->pipe);
  5923. spin_unlock_irqrestore(&dev->event_lock, flags);
  5924. wake_up_all(&dev_priv->pending_flip_queue);
  5925. queue_work(dev_priv->wq, &work->work);
  5926. trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
  5927. }
  5928. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  5929. {
  5930. drm_i915_private_t *dev_priv = dev->dev_private;
  5931. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  5932. do_intel_finish_page_flip(dev, crtc);
  5933. }
  5934. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  5935. {
  5936. drm_i915_private_t *dev_priv = dev->dev_private;
  5937. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  5938. do_intel_finish_page_flip(dev, crtc);
  5939. }
  5940. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  5941. {
  5942. drm_i915_private_t *dev_priv = dev->dev_private;
  5943. struct intel_crtc *intel_crtc =
  5944. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  5945. unsigned long flags;
  5946. /* NB: An MMIO update of the plane base pointer will also
  5947. * generate a page-flip completion irq, i.e. every modeset
  5948. * is also accompanied by a spurious intel_prepare_page_flip().
  5949. */
  5950. spin_lock_irqsave(&dev->event_lock, flags);
  5951. if (intel_crtc->unpin_work)
  5952. atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
  5953. spin_unlock_irqrestore(&dev->event_lock, flags);
  5954. }
  5955. inline static void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
  5956. {
  5957. /* Ensure that the work item is consistent when activating it ... */
  5958. smp_wmb();
  5959. atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
  5960. /* and that it is marked active as soon as the irq could fire. */
  5961. smp_wmb();
  5962. }
  5963. static int intel_gen2_queue_flip(struct drm_device *dev,
  5964. struct drm_crtc *crtc,
  5965. struct drm_framebuffer *fb,
  5966. struct drm_i915_gem_object *obj)
  5967. {
  5968. struct drm_i915_private *dev_priv = dev->dev_private;
  5969. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5970. u32 flip_mask;
  5971. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  5972. int ret;
  5973. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  5974. if (ret)
  5975. goto err;
  5976. ret = intel_ring_begin(ring, 6);
  5977. if (ret)
  5978. goto err_unpin;
  5979. /* Can't queue multiple flips, so wait for the previous
  5980. * one to finish before executing the next.
  5981. */
  5982. if (intel_crtc->plane)
  5983. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  5984. else
  5985. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  5986. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  5987. intel_ring_emit(ring, MI_NOOP);
  5988. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  5989. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  5990. intel_ring_emit(ring, fb->pitches[0]);
  5991. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  5992. intel_ring_emit(ring, 0); /* aux display base address, unused */
  5993. intel_mark_page_flip_active(intel_crtc);
  5994. intel_ring_advance(ring);
  5995. return 0;
  5996. err_unpin:
  5997. intel_unpin_fb_obj(obj);
  5998. err:
  5999. return ret;
  6000. }
  6001. static int intel_gen3_queue_flip(struct drm_device *dev,
  6002. struct drm_crtc *crtc,
  6003. struct drm_framebuffer *fb,
  6004. struct drm_i915_gem_object *obj)
  6005. {
  6006. struct drm_i915_private *dev_priv = dev->dev_private;
  6007. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6008. u32 flip_mask;
  6009. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6010. int ret;
  6011. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6012. if (ret)
  6013. goto err;
  6014. ret = intel_ring_begin(ring, 6);
  6015. if (ret)
  6016. goto err_unpin;
  6017. if (intel_crtc->plane)
  6018. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  6019. else
  6020. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  6021. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  6022. intel_ring_emit(ring, MI_NOOP);
  6023. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  6024. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6025. intel_ring_emit(ring, fb->pitches[0]);
  6026. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  6027. intel_ring_emit(ring, MI_NOOP);
  6028. intel_mark_page_flip_active(intel_crtc);
  6029. intel_ring_advance(ring);
  6030. return 0;
  6031. err_unpin:
  6032. intel_unpin_fb_obj(obj);
  6033. err:
  6034. return ret;
  6035. }
  6036. static int intel_gen4_queue_flip(struct drm_device *dev,
  6037. struct drm_crtc *crtc,
  6038. struct drm_framebuffer *fb,
  6039. struct drm_i915_gem_object *obj)
  6040. {
  6041. struct drm_i915_private *dev_priv = dev->dev_private;
  6042. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6043. uint32_t pf, pipesrc;
  6044. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6045. int ret;
  6046. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6047. if (ret)
  6048. goto err;
  6049. ret = intel_ring_begin(ring, 4);
  6050. if (ret)
  6051. goto err_unpin;
  6052. /* i965+ uses the linear or tiled offsets from the
  6053. * Display Registers (which do not change across a page-flip)
  6054. * so we need only reprogram the base address.
  6055. */
  6056. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6057. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6058. intel_ring_emit(ring, fb->pitches[0]);
  6059. intel_ring_emit(ring,
  6060. (obj->gtt_offset + intel_crtc->dspaddr_offset) |
  6061. obj->tiling_mode);
  6062. /* XXX Enabling the panel-fitter across page-flip is so far
  6063. * untested on non-native modes, so ignore it for now.
  6064. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  6065. */
  6066. pf = 0;
  6067. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6068. intel_ring_emit(ring, pf | pipesrc);
  6069. intel_mark_page_flip_active(intel_crtc);
  6070. intel_ring_advance(ring);
  6071. return 0;
  6072. err_unpin:
  6073. intel_unpin_fb_obj(obj);
  6074. err:
  6075. return ret;
  6076. }
  6077. static int intel_gen6_queue_flip(struct drm_device *dev,
  6078. struct drm_crtc *crtc,
  6079. struct drm_framebuffer *fb,
  6080. struct drm_i915_gem_object *obj)
  6081. {
  6082. struct drm_i915_private *dev_priv = dev->dev_private;
  6083. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6084. struct intel_ring_buffer *ring = &dev_priv->ring[RCS];
  6085. uint32_t pf, pipesrc;
  6086. int ret;
  6087. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6088. if (ret)
  6089. goto err;
  6090. ret = intel_ring_begin(ring, 4);
  6091. if (ret)
  6092. goto err_unpin;
  6093. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  6094. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  6095. intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
  6096. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  6097. /* Contrary to the suggestions in the documentation,
  6098. * "Enable Panel Fitter" does not seem to be required when page
  6099. * flipping with a non-native mode, and worse causes a normal
  6100. * modeset to fail.
  6101. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  6102. */
  6103. pf = 0;
  6104. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  6105. intel_ring_emit(ring, pf | pipesrc);
  6106. intel_mark_page_flip_active(intel_crtc);
  6107. intel_ring_advance(ring);
  6108. return 0;
  6109. err_unpin:
  6110. intel_unpin_fb_obj(obj);
  6111. err:
  6112. return ret;
  6113. }
  6114. /*
  6115. * On gen7 we currently use the blit ring because (in early silicon at least)
  6116. * the render ring doesn't give us interrpts for page flip completion, which
  6117. * means clients will hang after the first flip is queued. Fortunately the
  6118. * blit ring generates interrupts properly, so use it instead.
  6119. */
  6120. static int intel_gen7_queue_flip(struct drm_device *dev,
  6121. struct drm_crtc *crtc,
  6122. struct drm_framebuffer *fb,
  6123. struct drm_i915_gem_object *obj)
  6124. {
  6125. struct drm_i915_private *dev_priv = dev->dev_private;
  6126. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6127. struct intel_ring_buffer *ring = &dev_priv->ring[BCS];
  6128. uint32_t plane_bit = 0;
  6129. int ret;
  6130. ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
  6131. if (ret)
  6132. goto err;
  6133. switch(intel_crtc->plane) {
  6134. case PLANE_A:
  6135. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  6136. break;
  6137. case PLANE_B:
  6138. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  6139. break;
  6140. case PLANE_C:
  6141. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  6142. break;
  6143. default:
  6144. WARN_ONCE(1, "unknown plane in flip command\n");
  6145. ret = -ENODEV;
  6146. goto err_unpin;
  6147. }
  6148. ret = intel_ring_begin(ring, 4);
  6149. if (ret)
  6150. goto err_unpin;
  6151. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  6152. intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
  6153. intel_ring_emit(ring, obj->gtt_offset + intel_crtc->dspaddr_offset);
  6154. intel_ring_emit(ring, (MI_NOOP));
  6155. intel_mark_page_flip_active(intel_crtc);
  6156. intel_ring_advance(ring);
  6157. return 0;
  6158. err_unpin:
  6159. intel_unpin_fb_obj(obj);
  6160. err:
  6161. return ret;
  6162. }
  6163. static int intel_default_queue_flip(struct drm_device *dev,
  6164. struct drm_crtc *crtc,
  6165. struct drm_framebuffer *fb,
  6166. struct drm_i915_gem_object *obj)
  6167. {
  6168. return -ENODEV;
  6169. }
  6170. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  6171. struct drm_framebuffer *fb,
  6172. struct drm_pending_vblank_event *event)
  6173. {
  6174. struct drm_device *dev = crtc->dev;
  6175. struct drm_i915_private *dev_priv = dev->dev_private;
  6176. struct drm_framebuffer *old_fb = crtc->fb;
  6177. struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
  6178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6179. struct intel_unpin_work *work;
  6180. unsigned long flags;
  6181. int ret;
  6182. /* Can't change pixel format via MI display flips. */
  6183. if (fb->pixel_format != crtc->fb->pixel_format)
  6184. return -EINVAL;
  6185. /*
  6186. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  6187. * Note that pitch changes could also affect these register.
  6188. */
  6189. if (INTEL_INFO(dev)->gen > 3 &&
  6190. (fb->offsets[0] != crtc->fb->offsets[0] ||
  6191. fb->pitches[0] != crtc->fb->pitches[0]))
  6192. return -EINVAL;
  6193. work = kzalloc(sizeof *work, GFP_KERNEL);
  6194. if (work == NULL)
  6195. return -ENOMEM;
  6196. work->event = event;
  6197. work->crtc = crtc;
  6198. work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
  6199. INIT_WORK(&work->work, intel_unpin_work_fn);
  6200. ret = drm_vblank_get(dev, intel_crtc->pipe);
  6201. if (ret)
  6202. goto free_work;
  6203. /* We borrow the event spin lock for protecting unpin_work */
  6204. spin_lock_irqsave(&dev->event_lock, flags);
  6205. if (intel_crtc->unpin_work) {
  6206. spin_unlock_irqrestore(&dev->event_lock, flags);
  6207. kfree(work);
  6208. drm_vblank_put(dev, intel_crtc->pipe);
  6209. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  6210. return -EBUSY;
  6211. }
  6212. intel_crtc->unpin_work = work;
  6213. spin_unlock_irqrestore(&dev->event_lock, flags);
  6214. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  6215. flush_workqueue(dev_priv->wq);
  6216. ret = i915_mutex_lock_interruptible(dev);
  6217. if (ret)
  6218. goto cleanup;
  6219. /* Reference the objects for the scheduled work. */
  6220. drm_gem_object_reference(&work->old_fb_obj->base);
  6221. drm_gem_object_reference(&obj->base);
  6222. crtc->fb = fb;
  6223. work->pending_flip_obj = obj;
  6224. work->enable_stall_check = true;
  6225. atomic_inc(&intel_crtc->unpin_work_count);
  6226. intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  6227. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj);
  6228. if (ret)
  6229. goto cleanup_pending;
  6230. intel_disable_fbc(dev);
  6231. intel_mark_fb_busy(obj);
  6232. mutex_unlock(&dev->struct_mutex);
  6233. trace_i915_flip_request(intel_crtc->plane, obj);
  6234. return 0;
  6235. cleanup_pending:
  6236. atomic_dec(&intel_crtc->unpin_work_count);
  6237. crtc->fb = old_fb;
  6238. drm_gem_object_unreference(&work->old_fb_obj->base);
  6239. drm_gem_object_unreference(&obj->base);
  6240. mutex_unlock(&dev->struct_mutex);
  6241. cleanup:
  6242. spin_lock_irqsave(&dev->event_lock, flags);
  6243. intel_crtc->unpin_work = NULL;
  6244. spin_unlock_irqrestore(&dev->event_lock, flags);
  6245. drm_vblank_put(dev, intel_crtc->pipe);
  6246. free_work:
  6247. kfree(work);
  6248. return ret;
  6249. }
  6250. static struct drm_crtc_helper_funcs intel_helper_funcs = {
  6251. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  6252. .load_lut = intel_crtc_load_lut,
  6253. };
  6254. bool intel_encoder_check_is_cloned(struct intel_encoder *encoder)
  6255. {
  6256. struct intel_encoder *other_encoder;
  6257. struct drm_crtc *crtc = &encoder->new_crtc->base;
  6258. if (WARN_ON(!crtc))
  6259. return false;
  6260. list_for_each_entry(other_encoder,
  6261. &crtc->dev->mode_config.encoder_list,
  6262. base.head) {
  6263. if (&other_encoder->new_crtc->base != crtc ||
  6264. encoder == other_encoder)
  6265. continue;
  6266. else
  6267. return true;
  6268. }
  6269. return false;
  6270. }
  6271. static bool intel_encoder_crtc_ok(struct drm_encoder *encoder,
  6272. struct drm_crtc *crtc)
  6273. {
  6274. struct drm_device *dev;
  6275. struct drm_crtc *tmp;
  6276. int crtc_mask = 1;
  6277. WARN(!crtc, "checking null crtc?\n");
  6278. dev = crtc->dev;
  6279. list_for_each_entry(tmp, &dev->mode_config.crtc_list, head) {
  6280. if (tmp == crtc)
  6281. break;
  6282. crtc_mask <<= 1;
  6283. }
  6284. if (encoder->possible_crtcs & crtc_mask)
  6285. return true;
  6286. return false;
  6287. }
  6288. /**
  6289. * intel_modeset_update_staged_output_state
  6290. *
  6291. * Updates the staged output configuration state, e.g. after we've read out the
  6292. * current hw state.
  6293. */
  6294. static void intel_modeset_update_staged_output_state(struct drm_device *dev)
  6295. {
  6296. struct intel_encoder *encoder;
  6297. struct intel_connector *connector;
  6298. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6299. base.head) {
  6300. connector->new_encoder =
  6301. to_intel_encoder(connector->base.encoder);
  6302. }
  6303. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6304. base.head) {
  6305. encoder->new_crtc =
  6306. to_intel_crtc(encoder->base.crtc);
  6307. }
  6308. }
  6309. /**
  6310. * intel_modeset_commit_output_state
  6311. *
  6312. * This function copies the stage display pipe configuration to the real one.
  6313. */
  6314. static void intel_modeset_commit_output_state(struct drm_device *dev)
  6315. {
  6316. struct intel_encoder *encoder;
  6317. struct intel_connector *connector;
  6318. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6319. base.head) {
  6320. connector->base.encoder = &connector->new_encoder->base;
  6321. }
  6322. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6323. base.head) {
  6324. encoder->base.crtc = &encoder->new_crtc->base;
  6325. }
  6326. }
  6327. static int
  6328. pipe_config_set_bpp(struct drm_crtc *crtc,
  6329. struct drm_framebuffer *fb,
  6330. struct intel_crtc_config *pipe_config)
  6331. {
  6332. struct drm_device *dev = crtc->dev;
  6333. struct drm_connector *connector;
  6334. int bpp;
  6335. switch (fb->pixel_format) {
  6336. case DRM_FORMAT_C8:
  6337. bpp = 8*3; /* since we go through a colormap */
  6338. break;
  6339. case DRM_FORMAT_XRGB1555:
  6340. case DRM_FORMAT_ARGB1555:
  6341. /* checked in intel_framebuffer_init already */
  6342. if (WARN_ON(INTEL_INFO(dev)->gen > 3))
  6343. return -EINVAL;
  6344. case DRM_FORMAT_RGB565:
  6345. bpp = 6*3; /* min is 18bpp */
  6346. break;
  6347. case DRM_FORMAT_XBGR8888:
  6348. case DRM_FORMAT_ABGR8888:
  6349. /* checked in intel_framebuffer_init already */
  6350. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  6351. return -EINVAL;
  6352. case DRM_FORMAT_XRGB8888:
  6353. case DRM_FORMAT_ARGB8888:
  6354. bpp = 8*3;
  6355. break;
  6356. case DRM_FORMAT_XRGB2101010:
  6357. case DRM_FORMAT_ARGB2101010:
  6358. case DRM_FORMAT_XBGR2101010:
  6359. case DRM_FORMAT_ABGR2101010:
  6360. /* checked in intel_framebuffer_init already */
  6361. if (WARN_ON(INTEL_INFO(dev)->gen < 4))
  6362. return -EINVAL;
  6363. bpp = 10*3;
  6364. break;
  6365. /* TODO: gen4+ supports 16 bpc floating point, too. */
  6366. default:
  6367. DRM_DEBUG_KMS("unsupported depth\n");
  6368. return -EINVAL;
  6369. }
  6370. pipe_config->pipe_bpp = bpp;
  6371. /* Clamp display bpp to EDID value */
  6372. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6373. head) {
  6374. if (connector->encoder && connector->encoder->crtc != crtc)
  6375. continue;
  6376. /* Don't use an invalid EDID bpc value */
  6377. if (connector->display_info.bpc &&
  6378. connector->display_info.bpc * 3 < bpp) {
  6379. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  6380. bpp, connector->display_info.bpc*3);
  6381. pipe_config->pipe_bpp = connector->display_info.bpc*3;
  6382. }
  6383. }
  6384. return bpp;
  6385. }
  6386. static struct intel_crtc_config *
  6387. intel_modeset_pipe_config(struct drm_crtc *crtc,
  6388. struct drm_framebuffer *fb,
  6389. struct drm_display_mode *mode)
  6390. {
  6391. struct drm_device *dev = crtc->dev;
  6392. struct drm_encoder_helper_funcs *encoder_funcs;
  6393. struct intel_encoder *encoder;
  6394. struct intel_crtc_config *pipe_config;
  6395. int plane_bpp;
  6396. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6397. if (!pipe_config)
  6398. return ERR_PTR(-ENOMEM);
  6399. drm_mode_copy(&pipe_config->adjusted_mode, mode);
  6400. drm_mode_copy(&pipe_config->requested_mode, mode);
  6401. plane_bpp = pipe_config_set_bpp(crtc, fb, pipe_config);
  6402. if (plane_bpp < 0)
  6403. goto fail;
  6404. /* Pass our mode to the connectors and the CRTC to give them a chance to
  6405. * adjust it according to limitations or connector properties, and also
  6406. * a chance to reject the mode entirely.
  6407. */
  6408. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6409. base.head) {
  6410. if (&encoder->new_crtc->base != crtc)
  6411. continue;
  6412. if (encoder->compute_config) {
  6413. if (!(encoder->compute_config(encoder, pipe_config))) {
  6414. DRM_DEBUG_KMS("Encoder config failure\n");
  6415. goto fail;
  6416. }
  6417. continue;
  6418. }
  6419. encoder_funcs = encoder->base.helper_private;
  6420. if (!(encoder_funcs->mode_fixup(&encoder->base,
  6421. &pipe_config->requested_mode,
  6422. &pipe_config->adjusted_mode))) {
  6423. DRM_DEBUG_KMS("Encoder fixup failed\n");
  6424. goto fail;
  6425. }
  6426. }
  6427. if (!(intel_crtc_compute_config(crtc, pipe_config))) {
  6428. DRM_DEBUG_KMS("CRTC fixup failed\n");
  6429. goto fail;
  6430. }
  6431. DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  6432. pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
  6433. DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
  6434. plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  6435. return pipe_config;
  6436. fail:
  6437. kfree(pipe_config);
  6438. return ERR_PTR(-EINVAL);
  6439. }
  6440. /* Computes which crtcs are affected and sets the relevant bits in the mask. For
  6441. * simplicity we use the crtc's pipe number (because it's easier to obtain). */
  6442. static void
  6443. intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
  6444. unsigned *prepare_pipes, unsigned *disable_pipes)
  6445. {
  6446. struct intel_crtc *intel_crtc;
  6447. struct drm_device *dev = crtc->dev;
  6448. struct intel_encoder *encoder;
  6449. struct intel_connector *connector;
  6450. struct drm_crtc *tmp_crtc;
  6451. *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
  6452. /* Check which crtcs have changed outputs connected to them, these need
  6453. * to be part of the prepare_pipes mask. We don't (yet) support global
  6454. * modeset across multiple crtcs, so modeset_pipes will only have one
  6455. * bit set at most. */
  6456. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6457. base.head) {
  6458. if (connector->base.encoder == &connector->new_encoder->base)
  6459. continue;
  6460. if (connector->base.encoder) {
  6461. tmp_crtc = connector->base.encoder->crtc;
  6462. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  6463. }
  6464. if (connector->new_encoder)
  6465. *prepare_pipes |=
  6466. 1 << connector->new_encoder->new_crtc->pipe;
  6467. }
  6468. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6469. base.head) {
  6470. if (encoder->base.crtc == &encoder->new_crtc->base)
  6471. continue;
  6472. if (encoder->base.crtc) {
  6473. tmp_crtc = encoder->base.crtc;
  6474. *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
  6475. }
  6476. if (encoder->new_crtc)
  6477. *prepare_pipes |= 1 << encoder->new_crtc->pipe;
  6478. }
  6479. /* Check for any pipes that will be fully disabled ... */
  6480. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  6481. base.head) {
  6482. bool used = false;
  6483. /* Don't try to disable disabled crtcs. */
  6484. if (!intel_crtc->base.enabled)
  6485. continue;
  6486. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6487. base.head) {
  6488. if (encoder->new_crtc == intel_crtc)
  6489. used = true;
  6490. }
  6491. if (!used)
  6492. *disable_pipes |= 1 << intel_crtc->pipe;
  6493. }
  6494. /* set_mode is also used to update properties on life display pipes. */
  6495. intel_crtc = to_intel_crtc(crtc);
  6496. if (crtc->enabled)
  6497. *prepare_pipes |= 1 << intel_crtc->pipe;
  6498. /*
  6499. * For simplicity do a full modeset on any pipe where the output routing
  6500. * changed. We could be more clever, but that would require us to be
  6501. * more careful with calling the relevant encoder->mode_set functions.
  6502. */
  6503. if (*prepare_pipes)
  6504. *modeset_pipes = *prepare_pipes;
  6505. /* ... and mask these out. */
  6506. *modeset_pipes &= ~(*disable_pipes);
  6507. *prepare_pipes &= ~(*disable_pipes);
  6508. /*
  6509. * HACK: We don't (yet) fully support global modesets. intel_set_config
  6510. * obies this rule, but the modeset restore mode of
  6511. * intel_modeset_setup_hw_state does not.
  6512. */
  6513. *modeset_pipes &= 1 << intel_crtc->pipe;
  6514. *prepare_pipes &= 1 << intel_crtc->pipe;
  6515. }
  6516. static bool intel_crtc_in_use(struct drm_crtc *crtc)
  6517. {
  6518. struct drm_encoder *encoder;
  6519. struct drm_device *dev = crtc->dev;
  6520. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  6521. if (encoder->crtc == crtc)
  6522. return true;
  6523. return false;
  6524. }
  6525. static void
  6526. intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
  6527. {
  6528. struct intel_encoder *intel_encoder;
  6529. struct intel_crtc *intel_crtc;
  6530. struct drm_connector *connector;
  6531. list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
  6532. base.head) {
  6533. if (!intel_encoder->base.crtc)
  6534. continue;
  6535. intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
  6536. if (prepare_pipes & (1 << intel_crtc->pipe))
  6537. intel_encoder->connectors_active = false;
  6538. }
  6539. intel_modeset_commit_output_state(dev);
  6540. /* Update computed state. */
  6541. list_for_each_entry(intel_crtc, &dev->mode_config.crtc_list,
  6542. base.head) {
  6543. intel_crtc->base.enabled = intel_crtc_in_use(&intel_crtc->base);
  6544. }
  6545. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  6546. if (!connector->encoder || !connector->encoder->crtc)
  6547. continue;
  6548. intel_crtc = to_intel_crtc(connector->encoder->crtc);
  6549. if (prepare_pipes & (1 << intel_crtc->pipe)) {
  6550. struct drm_property *dpms_property =
  6551. dev->mode_config.dpms_property;
  6552. connector->dpms = DRM_MODE_DPMS_ON;
  6553. drm_object_property_set_value(&connector->base,
  6554. dpms_property,
  6555. DRM_MODE_DPMS_ON);
  6556. intel_encoder = to_intel_encoder(connector->encoder);
  6557. intel_encoder->connectors_active = true;
  6558. }
  6559. }
  6560. }
  6561. #define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
  6562. list_for_each_entry((intel_crtc), \
  6563. &(dev)->mode_config.crtc_list, \
  6564. base.head) \
  6565. if (mask & (1 <<(intel_crtc)->pipe)) \
  6566. static bool
  6567. intel_pipe_config_compare(struct intel_crtc_config *current_config,
  6568. struct intel_crtc_config *pipe_config)
  6569. {
  6570. if (current_config->has_pch_encoder != pipe_config->has_pch_encoder) {
  6571. DRM_ERROR("mismatch in has_pch_encoder "
  6572. "(expected %i, found %i)\n",
  6573. current_config->has_pch_encoder,
  6574. pipe_config->has_pch_encoder);
  6575. return false;
  6576. }
  6577. return true;
  6578. }
  6579. void
  6580. intel_modeset_check_state(struct drm_device *dev)
  6581. {
  6582. drm_i915_private_t *dev_priv = dev->dev_private;
  6583. struct intel_crtc *crtc;
  6584. struct intel_encoder *encoder;
  6585. struct intel_connector *connector;
  6586. struct intel_crtc_config pipe_config;
  6587. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6588. base.head) {
  6589. /* This also checks the encoder/connector hw state with the
  6590. * ->get_hw_state callbacks. */
  6591. intel_connector_check_state(connector);
  6592. WARN(&connector->new_encoder->base != connector->base.encoder,
  6593. "connector's staged encoder doesn't match current encoder\n");
  6594. }
  6595. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6596. base.head) {
  6597. bool enabled = false;
  6598. bool active = false;
  6599. enum pipe pipe, tracked_pipe;
  6600. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  6601. encoder->base.base.id,
  6602. drm_get_encoder_name(&encoder->base));
  6603. WARN(&encoder->new_crtc->base != encoder->base.crtc,
  6604. "encoder's stage crtc doesn't match current crtc\n");
  6605. WARN(encoder->connectors_active && !encoder->base.crtc,
  6606. "encoder's active_connectors set, but no crtc\n");
  6607. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6608. base.head) {
  6609. if (connector->base.encoder != &encoder->base)
  6610. continue;
  6611. enabled = true;
  6612. if (connector->base.dpms != DRM_MODE_DPMS_OFF)
  6613. active = true;
  6614. }
  6615. WARN(!!encoder->base.crtc != enabled,
  6616. "encoder's enabled state mismatch "
  6617. "(expected %i, found %i)\n",
  6618. !!encoder->base.crtc, enabled);
  6619. WARN(active && !encoder->base.crtc,
  6620. "active encoder with no crtc\n");
  6621. WARN(encoder->connectors_active != active,
  6622. "encoder's computed active state doesn't match tracked active state "
  6623. "(expected %i, found %i)\n", active, encoder->connectors_active);
  6624. active = encoder->get_hw_state(encoder, &pipe);
  6625. WARN(active != encoder->connectors_active,
  6626. "encoder's hw state doesn't match sw tracking "
  6627. "(expected %i, found %i)\n",
  6628. encoder->connectors_active, active);
  6629. if (!encoder->base.crtc)
  6630. continue;
  6631. tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
  6632. WARN(active && pipe != tracked_pipe,
  6633. "active encoder's pipe doesn't match"
  6634. "(expected %i, found %i)\n",
  6635. tracked_pipe, pipe);
  6636. }
  6637. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  6638. base.head) {
  6639. bool enabled = false;
  6640. bool active = false;
  6641. DRM_DEBUG_KMS("[CRTC:%d]\n",
  6642. crtc->base.base.id);
  6643. WARN(crtc->active && !crtc->base.enabled,
  6644. "active crtc, but not enabled in sw tracking\n");
  6645. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6646. base.head) {
  6647. if (encoder->base.crtc != &crtc->base)
  6648. continue;
  6649. enabled = true;
  6650. if (encoder->connectors_active)
  6651. active = true;
  6652. }
  6653. WARN(active != crtc->active,
  6654. "crtc's computed active state doesn't match tracked active state "
  6655. "(expected %i, found %i)\n", active, crtc->active);
  6656. WARN(enabled != crtc->base.enabled,
  6657. "crtc's computed enabled state doesn't match tracked enabled state "
  6658. "(expected %i, found %i)\n", enabled, crtc->base.enabled);
  6659. memset(&pipe_config, 0, sizeof(pipe_config));
  6660. active = dev_priv->display.get_pipe_config(crtc,
  6661. &pipe_config);
  6662. WARN(crtc->active != active,
  6663. "crtc active state doesn't match with hw state "
  6664. "(expected %i, found %i)\n", crtc->active, active);
  6665. WARN(active &&
  6666. !intel_pipe_config_compare(&crtc->config, &pipe_config),
  6667. "pipe state doesn't match!\n");
  6668. }
  6669. }
  6670. static int __intel_set_mode(struct drm_crtc *crtc,
  6671. struct drm_display_mode *mode,
  6672. int x, int y, struct drm_framebuffer *fb)
  6673. {
  6674. struct drm_device *dev = crtc->dev;
  6675. drm_i915_private_t *dev_priv = dev->dev_private;
  6676. struct drm_display_mode *saved_mode, *saved_hwmode;
  6677. struct intel_crtc_config *pipe_config = NULL;
  6678. struct intel_crtc *intel_crtc;
  6679. unsigned disable_pipes, prepare_pipes, modeset_pipes;
  6680. int ret = 0;
  6681. saved_mode = kmalloc(2 * sizeof(*saved_mode), GFP_KERNEL);
  6682. if (!saved_mode)
  6683. return -ENOMEM;
  6684. saved_hwmode = saved_mode + 1;
  6685. intel_modeset_affected_pipes(crtc, &modeset_pipes,
  6686. &prepare_pipes, &disable_pipes);
  6687. *saved_hwmode = crtc->hwmode;
  6688. *saved_mode = crtc->mode;
  6689. /* Hack: Because we don't (yet) support global modeset on multiple
  6690. * crtcs, we don't keep track of the new mode for more than one crtc.
  6691. * Hence simply check whether any bit is set in modeset_pipes in all the
  6692. * pieces of code that are not yet converted to deal with mutliple crtcs
  6693. * changing their mode at the same time. */
  6694. if (modeset_pipes) {
  6695. pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
  6696. if (IS_ERR(pipe_config)) {
  6697. ret = PTR_ERR(pipe_config);
  6698. pipe_config = NULL;
  6699. goto out;
  6700. }
  6701. }
  6702. DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
  6703. modeset_pipes, prepare_pipes, disable_pipes);
  6704. for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
  6705. intel_crtc_disable(&intel_crtc->base);
  6706. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
  6707. if (intel_crtc->base.enabled)
  6708. dev_priv->display.crtc_disable(&intel_crtc->base);
  6709. }
  6710. /* crtc->mode is already used by the ->mode_set callbacks, hence we need
  6711. * to set it here already despite that we pass it down the callchain.
  6712. */
  6713. if (modeset_pipes) {
  6714. enum transcoder tmp = to_intel_crtc(crtc)->config.cpu_transcoder;
  6715. crtc->mode = *mode;
  6716. /* mode_set/enable/disable functions rely on a correct pipe
  6717. * config. */
  6718. to_intel_crtc(crtc)->config = *pipe_config;
  6719. to_intel_crtc(crtc)->config.cpu_transcoder = tmp;
  6720. }
  6721. /* Only after disabling all output pipelines that will be changed can we
  6722. * update the the output configuration. */
  6723. intel_modeset_update_state(dev, prepare_pipes);
  6724. if (dev_priv->display.modeset_global_resources)
  6725. dev_priv->display.modeset_global_resources(dev);
  6726. /* Set up the DPLL and any encoders state that needs to adjust or depend
  6727. * on the DPLL.
  6728. */
  6729. for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
  6730. ret = intel_crtc_mode_set(&intel_crtc->base,
  6731. x, y, fb);
  6732. if (ret)
  6733. goto done;
  6734. }
  6735. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  6736. for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc)
  6737. dev_priv->display.crtc_enable(&intel_crtc->base);
  6738. if (modeset_pipes) {
  6739. /* Store real post-adjustment hardware mode. */
  6740. crtc->hwmode = pipe_config->adjusted_mode;
  6741. /* Calculate and store various constants which
  6742. * are later needed by vblank and swap-completion
  6743. * timestamping. They are derived from true hwmode.
  6744. */
  6745. drm_calc_timestamping_constants(crtc);
  6746. }
  6747. /* FIXME: add subpixel order */
  6748. done:
  6749. if (ret && crtc->enabled) {
  6750. crtc->hwmode = *saved_hwmode;
  6751. crtc->mode = *saved_mode;
  6752. }
  6753. out:
  6754. kfree(pipe_config);
  6755. kfree(saved_mode);
  6756. return ret;
  6757. }
  6758. int intel_set_mode(struct drm_crtc *crtc,
  6759. struct drm_display_mode *mode,
  6760. int x, int y, struct drm_framebuffer *fb)
  6761. {
  6762. int ret;
  6763. ret = __intel_set_mode(crtc, mode, x, y, fb);
  6764. if (ret == 0)
  6765. intel_modeset_check_state(crtc->dev);
  6766. return ret;
  6767. }
  6768. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  6769. {
  6770. intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->fb);
  6771. }
  6772. #undef for_each_intel_crtc_masked
  6773. static void intel_set_config_free(struct intel_set_config *config)
  6774. {
  6775. if (!config)
  6776. return;
  6777. kfree(config->save_connector_encoders);
  6778. kfree(config->save_encoder_crtcs);
  6779. kfree(config);
  6780. }
  6781. static int intel_set_config_save_state(struct drm_device *dev,
  6782. struct intel_set_config *config)
  6783. {
  6784. struct drm_encoder *encoder;
  6785. struct drm_connector *connector;
  6786. int count;
  6787. config->save_encoder_crtcs =
  6788. kcalloc(dev->mode_config.num_encoder,
  6789. sizeof(struct drm_crtc *), GFP_KERNEL);
  6790. if (!config->save_encoder_crtcs)
  6791. return -ENOMEM;
  6792. config->save_connector_encoders =
  6793. kcalloc(dev->mode_config.num_connector,
  6794. sizeof(struct drm_encoder *), GFP_KERNEL);
  6795. if (!config->save_connector_encoders)
  6796. return -ENOMEM;
  6797. /* Copy data. Note that driver private data is not affected.
  6798. * Should anything bad happen only the expected state is
  6799. * restored, not the drivers personal bookkeeping.
  6800. */
  6801. count = 0;
  6802. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  6803. config->save_encoder_crtcs[count++] = encoder->crtc;
  6804. }
  6805. count = 0;
  6806. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  6807. config->save_connector_encoders[count++] = connector->encoder;
  6808. }
  6809. return 0;
  6810. }
  6811. static void intel_set_config_restore_state(struct drm_device *dev,
  6812. struct intel_set_config *config)
  6813. {
  6814. struct intel_encoder *encoder;
  6815. struct intel_connector *connector;
  6816. int count;
  6817. count = 0;
  6818. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  6819. encoder->new_crtc =
  6820. to_intel_crtc(config->save_encoder_crtcs[count++]);
  6821. }
  6822. count = 0;
  6823. list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
  6824. connector->new_encoder =
  6825. to_intel_encoder(config->save_connector_encoders[count++]);
  6826. }
  6827. }
  6828. static void
  6829. intel_set_config_compute_mode_changes(struct drm_mode_set *set,
  6830. struct intel_set_config *config)
  6831. {
  6832. /* We should be able to check here if the fb has the same properties
  6833. * and then just flip_or_move it */
  6834. if (set->crtc->fb != set->fb) {
  6835. /* If we have no fb then treat it as a full mode set */
  6836. if (set->crtc->fb == NULL) {
  6837. DRM_DEBUG_KMS("crtc has no fb, full mode set\n");
  6838. config->mode_changed = true;
  6839. } else if (set->fb == NULL) {
  6840. config->mode_changed = true;
  6841. } else if (set->fb->pixel_format !=
  6842. set->crtc->fb->pixel_format) {
  6843. config->mode_changed = true;
  6844. } else
  6845. config->fb_changed = true;
  6846. }
  6847. if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
  6848. config->fb_changed = true;
  6849. if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
  6850. DRM_DEBUG_KMS("modes are different, full mode set\n");
  6851. drm_mode_debug_printmodeline(&set->crtc->mode);
  6852. drm_mode_debug_printmodeline(set->mode);
  6853. config->mode_changed = true;
  6854. }
  6855. }
  6856. static int
  6857. intel_modeset_stage_output_state(struct drm_device *dev,
  6858. struct drm_mode_set *set,
  6859. struct intel_set_config *config)
  6860. {
  6861. struct drm_crtc *new_crtc;
  6862. struct intel_connector *connector;
  6863. struct intel_encoder *encoder;
  6864. int count, ro;
  6865. /* The upper layers ensure that we either disable a crtc or have a list
  6866. * of connectors. For paranoia, double-check this. */
  6867. WARN_ON(!set->fb && (set->num_connectors != 0));
  6868. WARN_ON(set->fb && (set->num_connectors == 0));
  6869. count = 0;
  6870. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6871. base.head) {
  6872. /* Otherwise traverse passed in connector list and get encoders
  6873. * for them. */
  6874. for (ro = 0; ro < set->num_connectors; ro++) {
  6875. if (set->connectors[ro] == &connector->base) {
  6876. connector->new_encoder = connector->encoder;
  6877. break;
  6878. }
  6879. }
  6880. /* If we disable the crtc, disable all its connectors. Also, if
  6881. * the connector is on the changing crtc but not on the new
  6882. * connector list, disable it. */
  6883. if ((!set->fb || ro == set->num_connectors) &&
  6884. connector->base.encoder &&
  6885. connector->base.encoder->crtc == set->crtc) {
  6886. connector->new_encoder = NULL;
  6887. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
  6888. connector->base.base.id,
  6889. drm_get_connector_name(&connector->base));
  6890. }
  6891. if (&connector->new_encoder->base != connector->base.encoder) {
  6892. DRM_DEBUG_KMS("encoder changed, full mode switch\n");
  6893. config->mode_changed = true;
  6894. }
  6895. }
  6896. /* connector->new_encoder is now updated for all connectors. */
  6897. /* Update crtc of enabled connectors. */
  6898. count = 0;
  6899. list_for_each_entry(connector, &dev->mode_config.connector_list,
  6900. base.head) {
  6901. if (!connector->new_encoder)
  6902. continue;
  6903. new_crtc = connector->new_encoder->base.crtc;
  6904. for (ro = 0; ro < set->num_connectors; ro++) {
  6905. if (set->connectors[ro] == &connector->base)
  6906. new_crtc = set->crtc;
  6907. }
  6908. /* Make sure the new CRTC will work with the encoder */
  6909. if (!intel_encoder_crtc_ok(&connector->new_encoder->base,
  6910. new_crtc)) {
  6911. return -EINVAL;
  6912. }
  6913. connector->encoder->new_crtc = to_intel_crtc(new_crtc);
  6914. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
  6915. connector->base.base.id,
  6916. drm_get_connector_name(&connector->base),
  6917. new_crtc->base.id);
  6918. }
  6919. /* Check for any encoders that needs to be disabled. */
  6920. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  6921. base.head) {
  6922. list_for_each_entry(connector,
  6923. &dev->mode_config.connector_list,
  6924. base.head) {
  6925. if (connector->new_encoder == encoder) {
  6926. WARN_ON(!connector->new_encoder->new_crtc);
  6927. goto next_encoder;
  6928. }
  6929. }
  6930. encoder->new_crtc = NULL;
  6931. next_encoder:
  6932. /* Only now check for crtc changes so we don't miss encoders
  6933. * that will be disabled. */
  6934. if (&encoder->new_crtc->base != encoder->base.crtc) {
  6935. DRM_DEBUG_KMS("crtc changed, full mode switch\n");
  6936. config->mode_changed = true;
  6937. }
  6938. }
  6939. /* Now we've also updated encoder->new_crtc for all encoders. */
  6940. return 0;
  6941. }
  6942. static int intel_crtc_set_config(struct drm_mode_set *set)
  6943. {
  6944. struct drm_device *dev;
  6945. struct drm_mode_set save_set;
  6946. struct intel_set_config *config;
  6947. int ret;
  6948. BUG_ON(!set);
  6949. BUG_ON(!set->crtc);
  6950. BUG_ON(!set->crtc->helper_private);
  6951. /* Enforce sane interface api - has been abused by the fb helper. */
  6952. BUG_ON(!set->mode && set->fb);
  6953. BUG_ON(set->fb && set->num_connectors == 0);
  6954. if (set->fb) {
  6955. DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
  6956. set->crtc->base.id, set->fb->base.id,
  6957. (int)set->num_connectors, set->x, set->y);
  6958. } else {
  6959. DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
  6960. }
  6961. dev = set->crtc->dev;
  6962. ret = -ENOMEM;
  6963. config = kzalloc(sizeof(*config), GFP_KERNEL);
  6964. if (!config)
  6965. goto out_config;
  6966. ret = intel_set_config_save_state(dev, config);
  6967. if (ret)
  6968. goto out_config;
  6969. save_set.crtc = set->crtc;
  6970. save_set.mode = &set->crtc->mode;
  6971. save_set.x = set->crtc->x;
  6972. save_set.y = set->crtc->y;
  6973. save_set.fb = set->crtc->fb;
  6974. /* Compute whether we need a full modeset, only an fb base update or no
  6975. * change at all. In the future we might also check whether only the
  6976. * mode changed, e.g. for LVDS where we only change the panel fitter in
  6977. * such cases. */
  6978. intel_set_config_compute_mode_changes(set, config);
  6979. ret = intel_modeset_stage_output_state(dev, set, config);
  6980. if (ret)
  6981. goto fail;
  6982. if (config->mode_changed) {
  6983. if (set->mode) {
  6984. DRM_DEBUG_KMS("attempting to set mode from"
  6985. " userspace\n");
  6986. drm_mode_debug_printmodeline(set->mode);
  6987. }
  6988. ret = intel_set_mode(set->crtc, set->mode,
  6989. set->x, set->y, set->fb);
  6990. if (ret) {
  6991. DRM_ERROR("failed to set mode on [CRTC:%d], err = %d\n",
  6992. set->crtc->base.id, ret);
  6993. goto fail;
  6994. }
  6995. } else if (config->fb_changed) {
  6996. intel_crtc_wait_for_pending_flips(set->crtc);
  6997. ret = intel_pipe_set_base(set->crtc,
  6998. set->x, set->y, set->fb);
  6999. }
  7000. intel_set_config_free(config);
  7001. return 0;
  7002. fail:
  7003. intel_set_config_restore_state(dev, config);
  7004. /* Try to restore the config */
  7005. if (config->mode_changed &&
  7006. intel_set_mode(save_set.crtc, save_set.mode,
  7007. save_set.x, save_set.y, save_set.fb))
  7008. DRM_ERROR("failed to restore config after modeset failure\n");
  7009. out_config:
  7010. intel_set_config_free(config);
  7011. return ret;
  7012. }
  7013. static const struct drm_crtc_funcs intel_crtc_funcs = {
  7014. .cursor_set = intel_crtc_cursor_set,
  7015. .cursor_move = intel_crtc_cursor_move,
  7016. .gamma_set = intel_crtc_gamma_set,
  7017. .set_config = intel_crtc_set_config,
  7018. .destroy = intel_crtc_destroy,
  7019. .page_flip = intel_crtc_page_flip,
  7020. };
  7021. static void intel_cpu_pll_init(struct drm_device *dev)
  7022. {
  7023. if (HAS_DDI(dev))
  7024. intel_ddi_pll_init(dev);
  7025. }
  7026. static void intel_pch_pll_init(struct drm_device *dev)
  7027. {
  7028. drm_i915_private_t *dev_priv = dev->dev_private;
  7029. int i;
  7030. if (dev_priv->num_pch_pll == 0) {
  7031. DRM_DEBUG_KMS("No PCH PLLs on this hardware, skipping initialisation\n");
  7032. return;
  7033. }
  7034. for (i = 0; i < dev_priv->num_pch_pll; i++) {
  7035. dev_priv->pch_plls[i].pll_reg = _PCH_DPLL(i);
  7036. dev_priv->pch_plls[i].fp0_reg = _PCH_FP0(i);
  7037. dev_priv->pch_plls[i].fp1_reg = _PCH_FP1(i);
  7038. }
  7039. }
  7040. static void intel_crtc_init(struct drm_device *dev, int pipe)
  7041. {
  7042. drm_i915_private_t *dev_priv = dev->dev_private;
  7043. struct intel_crtc *intel_crtc;
  7044. int i;
  7045. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  7046. if (intel_crtc == NULL)
  7047. return;
  7048. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  7049. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  7050. for (i = 0; i < 256; i++) {
  7051. intel_crtc->lut_r[i] = i;
  7052. intel_crtc->lut_g[i] = i;
  7053. intel_crtc->lut_b[i] = i;
  7054. }
  7055. /* Swap pipes & planes for FBC on pre-965 */
  7056. intel_crtc->pipe = pipe;
  7057. intel_crtc->plane = pipe;
  7058. intel_crtc->config.cpu_transcoder = pipe;
  7059. if (IS_MOBILE(dev) && IS_GEN3(dev)) {
  7060. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  7061. intel_crtc->plane = !pipe;
  7062. }
  7063. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  7064. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  7065. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  7066. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  7067. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  7068. }
  7069. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  7070. struct drm_file *file)
  7071. {
  7072. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  7073. struct drm_mode_object *drmmode_obj;
  7074. struct intel_crtc *crtc;
  7075. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  7076. return -ENODEV;
  7077. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  7078. DRM_MODE_OBJECT_CRTC);
  7079. if (!drmmode_obj) {
  7080. DRM_ERROR("no such CRTC id\n");
  7081. return -EINVAL;
  7082. }
  7083. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  7084. pipe_from_crtc_id->pipe = crtc->pipe;
  7085. return 0;
  7086. }
  7087. static int intel_encoder_clones(struct intel_encoder *encoder)
  7088. {
  7089. struct drm_device *dev = encoder->base.dev;
  7090. struct intel_encoder *source_encoder;
  7091. int index_mask = 0;
  7092. int entry = 0;
  7093. list_for_each_entry(source_encoder,
  7094. &dev->mode_config.encoder_list, base.head) {
  7095. if (encoder == source_encoder)
  7096. index_mask |= (1 << entry);
  7097. /* Intel hw has only one MUX where enocoders could be cloned. */
  7098. if (encoder->cloneable && source_encoder->cloneable)
  7099. index_mask |= (1 << entry);
  7100. entry++;
  7101. }
  7102. return index_mask;
  7103. }
  7104. static bool has_edp_a(struct drm_device *dev)
  7105. {
  7106. struct drm_i915_private *dev_priv = dev->dev_private;
  7107. if (!IS_MOBILE(dev))
  7108. return false;
  7109. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  7110. return false;
  7111. if (IS_GEN5(dev) &&
  7112. (I915_READ(ILK_DISPLAY_CHICKEN_FUSES) & ILK_eDP_A_DISABLE))
  7113. return false;
  7114. return true;
  7115. }
  7116. static void intel_setup_outputs(struct drm_device *dev)
  7117. {
  7118. struct drm_i915_private *dev_priv = dev->dev_private;
  7119. struct intel_encoder *encoder;
  7120. bool dpd_is_edp = false;
  7121. bool has_lvds;
  7122. has_lvds = intel_lvds_init(dev);
  7123. if (!has_lvds && !HAS_PCH_SPLIT(dev)) {
  7124. /* disable the panel fitter on everything but LVDS */
  7125. I915_WRITE(PFIT_CONTROL, 0);
  7126. }
  7127. if (!IS_ULT(dev))
  7128. intel_crt_init(dev);
  7129. if (HAS_DDI(dev)) {
  7130. int found;
  7131. /* Haswell uses DDI functions to detect digital outputs */
  7132. found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
  7133. /* DDI A only supports eDP */
  7134. if (found)
  7135. intel_ddi_init(dev, PORT_A);
  7136. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  7137. * register */
  7138. found = I915_READ(SFUSE_STRAP);
  7139. if (found & SFUSE_STRAP_DDIB_DETECTED)
  7140. intel_ddi_init(dev, PORT_B);
  7141. if (found & SFUSE_STRAP_DDIC_DETECTED)
  7142. intel_ddi_init(dev, PORT_C);
  7143. if (found & SFUSE_STRAP_DDID_DETECTED)
  7144. intel_ddi_init(dev, PORT_D);
  7145. } else if (HAS_PCH_SPLIT(dev)) {
  7146. int found;
  7147. dpd_is_edp = intel_dpd_is_edp(dev);
  7148. if (has_edp_a(dev))
  7149. intel_dp_init(dev, DP_A, PORT_A);
  7150. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  7151. /* PCH SDVOB multiplex with HDMIB */
  7152. found = intel_sdvo_init(dev, PCH_SDVOB, true);
  7153. if (!found)
  7154. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  7155. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  7156. intel_dp_init(dev, PCH_DP_B, PORT_B);
  7157. }
  7158. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  7159. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  7160. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  7161. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  7162. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  7163. intel_dp_init(dev, PCH_DP_C, PORT_C);
  7164. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  7165. intel_dp_init(dev, PCH_DP_D, PORT_D);
  7166. } else if (IS_VALLEYVIEW(dev)) {
  7167. /* Check for built-in panel first. Shares lanes with HDMI on SDVOC */
  7168. if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
  7169. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
  7170. if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
  7171. intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
  7172. PORT_B);
  7173. if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
  7174. intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
  7175. }
  7176. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  7177. bool found = false;
  7178. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  7179. DRM_DEBUG_KMS("probing SDVOB\n");
  7180. found = intel_sdvo_init(dev, GEN3_SDVOB, true);
  7181. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  7182. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  7183. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  7184. }
  7185. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  7186. DRM_DEBUG_KMS("probing DP_B\n");
  7187. intel_dp_init(dev, DP_B, PORT_B);
  7188. }
  7189. }
  7190. /* Before G4X SDVOC doesn't have its own detect register */
  7191. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  7192. DRM_DEBUG_KMS("probing SDVOC\n");
  7193. found = intel_sdvo_init(dev, GEN3_SDVOC, false);
  7194. }
  7195. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  7196. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  7197. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  7198. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  7199. }
  7200. if (SUPPORTS_INTEGRATED_DP(dev)) {
  7201. DRM_DEBUG_KMS("probing DP_C\n");
  7202. intel_dp_init(dev, DP_C, PORT_C);
  7203. }
  7204. }
  7205. if (SUPPORTS_INTEGRATED_DP(dev) &&
  7206. (I915_READ(DP_D) & DP_DETECTED)) {
  7207. DRM_DEBUG_KMS("probing DP_D\n");
  7208. intel_dp_init(dev, DP_D, PORT_D);
  7209. }
  7210. } else if (IS_GEN2(dev))
  7211. intel_dvo_init(dev);
  7212. if (SUPPORTS_TV(dev))
  7213. intel_tv_init(dev);
  7214. list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
  7215. encoder->base.possible_crtcs = encoder->crtc_mask;
  7216. encoder->base.possible_clones =
  7217. intel_encoder_clones(encoder);
  7218. }
  7219. intel_init_pch_refclk(dev);
  7220. drm_helper_move_panel_connectors_to_head(dev);
  7221. }
  7222. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  7223. {
  7224. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7225. drm_framebuffer_cleanup(fb);
  7226. drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
  7227. kfree(intel_fb);
  7228. }
  7229. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  7230. struct drm_file *file,
  7231. unsigned int *handle)
  7232. {
  7233. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  7234. struct drm_i915_gem_object *obj = intel_fb->obj;
  7235. return drm_gem_handle_create(file, &obj->base, handle);
  7236. }
  7237. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  7238. .destroy = intel_user_framebuffer_destroy,
  7239. .create_handle = intel_user_framebuffer_create_handle,
  7240. };
  7241. int intel_framebuffer_init(struct drm_device *dev,
  7242. struct intel_framebuffer *intel_fb,
  7243. struct drm_mode_fb_cmd2 *mode_cmd,
  7244. struct drm_i915_gem_object *obj)
  7245. {
  7246. int ret;
  7247. if (obj->tiling_mode == I915_TILING_Y) {
  7248. DRM_DEBUG("hardware does not support tiling Y\n");
  7249. return -EINVAL;
  7250. }
  7251. if (mode_cmd->pitches[0] & 63) {
  7252. DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
  7253. mode_cmd->pitches[0]);
  7254. return -EINVAL;
  7255. }
  7256. /* FIXME <= Gen4 stride limits are bit unclear */
  7257. if (mode_cmd->pitches[0] > 32768) {
  7258. DRM_DEBUG("pitch (%d) must be at less than 32768\n",
  7259. mode_cmd->pitches[0]);
  7260. return -EINVAL;
  7261. }
  7262. if (obj->tiling_mode != I915_TILING_NONE &&
  7263. mode_cmd->pitches[0] != obj->stride) {
  7264. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  7265. mode_cmd->pitches[0], obj->stride);
  7266. return -EINVAL;
  7267. }
  7268. /* Reject formats not supported by any plane early. */
  7269. switch (mode_cmd->pixel_format) {
  7270. case DRM_FORMAT_C8:
  7271. case DRM_FORMAT_RGB565:
  7272. case DRM_FORMAT_XRGB8888:
  7273. case DRM_FORMAT_ARGB8888:
  7274. break;
  7275. case DRM_FORMAT_XRGB1555:
  7276. case DRM_FORMAT_ARGB1555:
  7277. if (INTEL_INFO(dev)->gen > 3) {
  7278. DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
  7279. return -EINVAL;
  7280. }
  7281. break;
  7282. case DRM_FORMAT_XBGR8888:
  7283. case DRM_FORMAT_ABGR8888:
  7284. case DRM_FORMAT_XRGB2101010:
  7285. case DRM_FORMAT_ARGB2101010:
  7286. case DRM_FORMAT_XBGR2101010:
  7287. case DRM_FORMAT_ABGR2101010:
  7288. if (INTEL_INFO(dev)->gen < 4) {
  7289. DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
  7290. return -EINVAL;
  7291. }
  7292. break;
  7293. case DRM_FORMAT_YUYV:
  7294. case DRM_FORMAT_UYVY:
  7295. case DRM_FORMAT_YVYU:
  7296. case DRM_FORMAT_VYUY:
  7297. if (INTEL_INFO(dev)->gen < 5) {
  7298. DRM_DEBUG("invalid format: 0x%08x\n", mode_cmd->pixel_format);
  7299. return -EINVAL;
  7300. }
  7301. break;
  7302. default:
  7303. DRM_DEBUG("unsupported pixel format 0x%08x\n", mode_cmd->pixel_format);
  7304. return -EINVAL;
  7305. }
  7306. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  7307. if (mode_cmd->offsets[0] != 0)
  7308. return -EINVAL;
  7309. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  7310. intel_fb->obj = obj;
  7311. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  7312. if (ret) {
  7313. DRM_ERROR("framebuffer init failed %d\n", ret);
  7314. return ret;
  7315. }
  7316. return 0;
  7317. }
  7318. static struct drm_framebuffer *
  7319. intel_user_framebuffer_create(struct drm_device *dev,
  7320. struct drm_file *filp,
  7321. struct drm_mode_fb_cmd2 *mode_cmd)
  7322. {
  7323. struct drm_i915_gem_object *obj;
  7324. obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
  7325. mode_cmd->handles[0]));
  7326. if (&obj->base == NULL)
  7327. return ERR_PTR(-ENOENT);
  7328. return intel_framebuffer_create(dev, mode_cmd, obj);
  7329. }
  7330. static const struct drm_mode_config_funcs intel_mode_funcs = {
  7331. .fb_create = intel_user_framebuffer_create,
  7332. .output_poll_changed = intel_fb_output_poll_changed,
  7333. };
  7334. /* Set up chip specific display functions */
  7335. static void intel_init_display(struct drm_device *dev)
  7336. {
  7337. struct drm_i915_private *dev_priv = dev->dev_private;
  7338. if (HAS_DDI(dev)) {
  7339. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  7340. dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
  7341. dev_priv->display.crtc_enable = haswell_crtc_enable;
  7342. dev_priv->display.crtc_disable = haswell_crtc_disable;
  7343. dev_priv->display.off = haswell_crtc_off;
  7344. dev_priv->display.update_plane = ironlake_update_plane;
  7345. } else if (HAS_PCH_SPLIT(dev)) {
  7346. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  7347. dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
  7348. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  7349. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  7350. dev_priv->display.off = ironlake_crtc_off;
  7351. dev_priv->display.update_plane = ironlake_update_plane;
  7352. } else {
  7353. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  7354. dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
  7355. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  7356. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  7357. dev_priv->display.off = i9xx_crtc_off;
  7358. dev_priv->display.update_plane = i9xx_update_plane;
  7359. }
  7360. /* Returns the core display clock speed */
  7361. if (IS_VALLEYVIEW(dev))
  7362. dev_priv->display.get_display_clock_speed =
  7363. valleyview_get_display_clock_speed;
  7364. else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
  7365. dev_priv->display.get_display_clock_speed =
  7366. i945_get_display_clock_speed;
  7367. else if (IS_I915G(dev))
  7368. dev_priv->display.get_display_clock_speed =
  7369. i915_get_display_clock_speed;
  7370. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  7371. dev_priv->display.get_display_clock_speed =
  7372. i9xx_misc_get_display_clock_speed;
  7373. else if (IS_I915GM(dev))
  7374. dev_priv->display.get_display_clock_speed =
  7375. i915gm_get_display_clock_speed;
  7376. else if (IS_I865G(dev))
  7377. dev_priv->display.get_display_clock_speed =
  7378. i865_get_display_clock_speed;
  7379. else if (IS_I85X(dev))
  7380. dev_priv->display.get_display_clock_speed =
  7381. i855_get_display_clock_speed;
  7382. else /* 852, 830 */
  7383. dev_priv->display.get_display_clock_speed =
  7384. i830_get_display_clock_speed;
  7385. if (HAS_PCH_SPLIT(dev)) {
  7386. if (IS_GEN5(dev)) {
  7387. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  7388. dev_priv->display.write_eld = ironlake_write_eld;
  7389. } else if (IS_GEN6(dev)) {
  7390. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  7391. dev_priv->display.write_eld = ironlake_write_eld;
  7392. } else if (IS_IVYBRIDGE(dev)) {
  7393. /* FIXME: detect B0+ stepping and use auto training */
  7394. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  7395. dev_priv->display.write_eld = ironlake_write_eld;
  7396. dev_priv->display.modeset_global_resources =
  7397. ivb_modeset_global_resources;
  7398. } else if (IS_HASWELL(dev)) {
  7399. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  7400. dev_priv->display.write_eld = haswell_write_eld;
  7401. dev_priv->display.modeset_global_resources =
  7402. haswell_modeset_global_resources;
  7403. }
  7404. } else if (IS_G4X(dev)) {
  7405. dev_priv->display.write_eld = g4x_write_eld;
  7406. }
  7407. /* Default just returns -ENODEV to indicate unsupported */
  7408. dev_priv->display.queue_flip = intel_default_queue_flip;
  7409. switch (INTEL_INFO(dev)->gen) {
  7410. case 2:
  7411. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  7412. break;
  7413. case 3:
  7414. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  7415. break;
  7416. case 4:
  7417. case 5:
  7418. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  7419. break;
  7420. case 6:
  7421. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  7422. break;
  7423. case 7:
  7424. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  7425. break;
  7426. }
  7427. }
  7428. /*
  7429. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  7430. * resume, or other times. This quirk makes sure that's the case for
  7431. * affected systems.
  7432. */
  7433. static void quirk_pipea_force(struct drm_device *dev)
  7434. {
  7435. struct drm_i915_private *dev_priv = dev->dev_private;
  7436. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  7437. DRM_INFO("applying pipe a force quirk\n");
  7438. }
  7439. /*
  7440. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  7441. */
  7442. static void quirk_ssc_force_disable(struct drm_device *dev)
  7443. {
  7444. struct drm_i915_private *dev_priv = dev->dev_private;
  7445. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  7446. DRM_INFO("applying lvds SSC disable quirk\n");
  7447. }
  7448. /*
  7449. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  7450. * brightness value
  7451. */
  7452. static void quirk_invert_brightness(struct drm_device *dev)
  7453. {
  7454. struct drm_i915_private *dev_priv = dev->dev_private;
  7455. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  7456. DRM_INFO("applying inverted panel brightness quirk\n");
  7457. }
  7458. struct intel_quirk {
  7459. int device;
  7460. int subsystem_vendor;
  7461. int subsystem_device;
  7462. void (*hook)(struct drm_device *dev);
  7463. };
  7464. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  7465. struct intel_dmi_quirk {
  7466. void (*hook)(struct drm_device *dev);
  7467. const struct dmi_system_id (*dmi_id_list)[];
  7468. };
  7469. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  7470. {
  7471. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  7472. return 1;
  7473. }
  7474. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  7475. {
  7476. .dmi_id_list = &(const struct dmi_system_id[]) {
  7477. {
  7478. .callback = intel_dmi_reverse_brightness,
  7479. .ident = "NCR Corporation",
  7480. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  7481. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  7482. },
  7483. },
  7484. { } /* terminating entry */
  7485. },
  7486. .hook = quirk_invert_brightness,
  7487. },
  7488. };
  7489. static struct intel_quirk intel_quirks[] = {
  7490. /* HP Mini needs pipe A force quirk (LP: #322104) */
  7491. { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
  7492. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  7493. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  7494. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  7495. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  7496. /* 830/845 need to leave pipe A & dpll A up */
  7497. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7498. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  7499. /* Lenovo U160 cannot use SSC on LVDS */
  7500. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  7501. /* Sony Vaio Y cannot use SSC on LVDS */
  7502. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  7503. /* Acer Aspire 5734Z must invert backlight brightness */
  7504. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  7505. /* Acer/eMachines G725 */
  7506. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  7507. /* Acer/eMachines e725 */
  7508. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  7509. /* Acer/Packard Bell NCL20 */
  7510. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  7511. /* Acer Aspire 4736Z */
  7512. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  7513. };
  7514. static void intel_init_quirks(struct drm_device *dev)
  7515. {
  7516. struct pci_dev *d = dev->pdev;
  7517. int i;
  7518. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  7519. struct intel_quirk *q = &intel_quirks[i];
  7520. if (d->device == q->device &&
  7521. (d->subsystem_vendor == q->subsystem_vendor ||
  7522. q->subsystem_vendor == PCI_ANY_ID) &&
  7523. (d->subsystem_device == q->subsystem_device ||
  7524. q->subsystem_device == PCI_ANY_ID))
  7525. q->hook(dev);
  7526. }
  7527. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  7528. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  7529. intel_dmi_quirks[i].hook(dev);
  7530. }
  7531. }
  7532. /* Disable the VGA plane that we never use */
  7533. static void i915_disable_vga(struct drm_device *dev)
  7534. {
  7535. struct drm_i915_private *dev_priv = dev->dev_private;
  7536. u8 sr1;
  7537. u32 vga_reg = i915_vgacntrl_reg(dev);
  7538. vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
  7539. outb(SR01, VGA_SR_INDEX);
  7540. sr1 = inb(VGA_SR_DATA);
  7541. outb(sr1 | 1<<5, VGA_SR_DATA);
  7542. vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
  7543. udelay(300);
  7544. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  7545. POSTING_READ(vga_reg);
  7546. }
  7547. void intel_modeset_init_hw(struct drm_device *dev)
  7548. {
  7549. intel_init_power_well(dev);
  7550. intel_prepare_ddi(dev);
  7551. intel_init_clock_gating(dev);
  7552. mutex_lock(&dev->struct_mutex);
  7553. intel_enable_gt_powersave(dev);
  7554. mutex_unlock(&dev->struct_mutex);
  7555. }
  7556. void intel_modeset_init(struct drm_device *dev)
  7557. {
  7558. struct drm_i915_private *dev_priv = dev->dev_private;
  7559. int i, j, ret;
  7560. drm_mode_config_init(dev);
  7561. dev->mode_config.min_width = 0;
  7562. dev->mode_config.min_height = 0;
  7563. dev->mode_config.preferred_depth = 24;
  7564. dev->mode_config.prefer_shadow = 1;
  7565. dev->mode_config.funcs = &intel_mode_funcs;
  7566. intel_init_quirks(dev);
  7567. intel_init_pm(dev);
  7568. if (INTEL_INFO(dev)->num_pipes == 0)
  7569. return;
  7570. intel_init_display(dev);
  7571. if (IS_GEN2(dev)) {
  7572. dev->mode_config.max_width = 2048;
  7573. dev->mode_config.max_height = 2048;
  7574. } else if (IS_GEN3(dev)) {
  7575. dev->mode_config.max_width = 4096;
  7576. dev->mode_config.max_height = 4096;
  7577. } else {
  7578. dev->mode_config.max_width = 8192;
  7579. dev->mode_config.max_height = 8192;
  7580. }
  7581. dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
  7582. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  7583. INTEL_INFO(dev)->num_pipes,
  7584. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  7585. for (i = 0; i < INTEL_INFO(dev)->num_pipes; i++) {
  7586. intel_crtc_init(dev, i);
  7587. for (j = 0; j < dev_priv->num_plane; j++) {
  7588. ret = intel_plane_init(dev, i, j);
  7589. if (ret)
  7590. DRM_DEBUG_KMS("pipe %d plane %d init failed: %d\n",
  7591. i, j, ret);
  7592. }
  7593. }
  7594. intel_cpu_pll_init(dev);
  7595. intel_pch_pll_init(dev);
  7596. /* Just disable it once at startup */
  7597. i915_disable_vga(dev);
  7598. intel_setup_outputs(dev);
  7599. /* Just in case the BIOS is doing something questionable. */
  7600. intel_disable_fbc(dev);
  7601. }
  7602. static void
  7603. intel_connector_break_all_links(struct intel_connector *connector)
  7604. {
  7605. connector->base.dpms = DRM_MODE_DPMS_OFF;
  7606. connector->base.encoder = NULL;
  7607. connector->encoder->connectors_active = false;
  7608. connector->encoder->base.crtc = NULL;
  7609. }
  7610. static void intel_enable_pipe_a(struct drm_device *dev)
  7611. {
  7612. struct intel_connector *connector;
  7613. struct drm_connector *crt = NULL;
  7614. struct intel_load_detect_pipe load_detect_temp;
  7615. /* We can't just switch on the pipe A, we need to set things up with a
  7616. * proper mode and output configuration. As a gross hack, enable pipe A
  7617. * by enabling the load detect pipe once. */
  7618. list_for_each_entry(connector,
  7619. &dev->mode_config.connector_list,
  7620. base.head) {
  7621. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  7622. crt = &connector->base;
  7623. break;
  7624. }
  7625. }
  7626. if (!crt)
  7627. return;
  7628. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp))
  7629. intel_release_load_detect_pipe(crt, &load_detect_temp);
  7630. }
  7631. static bool
  7632. intel_check_plane_mapping(struct intel_crtc *crtc)
  7633. {
  7634. struct drm_device *dev = crtc->base.dev;
  7635. struct drm_i915_private *dev_priv = dev->dev_private;
  7636. u32 reg, val;
  7637. if (INTEL_INFO(dev)->num_pipes == 1)
  7638. return true;
  7639. reg = DSPCNTR(!crtc->plane);
  7640. val = I915_READ(reg);
  7641. if ((val & DISPLAY_PLANE_ENABLE) &&
  7642. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  7643. return false;
  7644. return true;
  7645. }
  7646. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  7647. {
  7648. struct drm_device *dev = crtc->base.dev;
  7649. struct drm_i915_private *dev_priv = dev->dev_private;
  7650. u32 reg;
  7651. /* Clear any frame start delays used for debugging left by the BIOS */
  7652. reg = PIPECONF(crtc->config.cpu_transcoder);
  7653. I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  7654. /* We need to sanitize the plane -> pipe mapping first because this will
  7655. * disable the crtc (and hence change the state) if it is wrong. Note
  7656. * that gen4+ has a fixed plane -> pipe mapping. */
  7657. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  7658. struct intel_connector *connector;
  7659. bool plane;
  7660. DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
  7661. crtc->base.base.id);
  7662. /* Pipe has the wrong plane attached and the plane is active.
  7663. * Temporarily change the plane mapping and disable everything
  7664. * ... */
  7665. plane = crtc->plane;
  7666. crtc->plane = !plane;
  7667. dev_priv->display.crtc_disable(&crtc->base);
  7668. crtc->plane = plane;
  7669. /* ... and break all links. */
  7670. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7671. base.head) {
  7672. if (connector->encoder->base.crtc != &crtc->base)
  7673. continue;
  7674. intel_connector_break_all_links(connector);
  7675. }
  7676. WARN_ON(crtc->active);
  7677. crtc->base.enabled = false;
  7678. }
  7679. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  7680. crtc->pipe == PIPE_A && !crtc->active) {
  7681. /* BIOS forgot to enable pipe A, this mostly happens after
  7682. * resume. Force-enable the pipe to fix this, the update_dpms
  7683. * call below we restore the pipe to the right state, but leave
  7684. * the required bits on. */
  7685. intel_enable_pipe_a(dev);
  7686. }
  7687. /* Adjust the state of the output pipe according to whether we
  7688. * have active connectors/encoders. */
  7689. intel_crtc_update_dpms(&crtc->base);
  7690. if (crtc->active != crtc->base.enabled) {
  7691. struct intel_encoder *encoder;
  7692. /* This can happen either due to bugs in the get_hw_state
  7693. * functions or because the pipe is force-enabled due to the
  7694. * pipe A quirk. */
  7695. DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
  7696. crtc->base.base.id,
  7697. crtc->base.enabled ? "enabled" : "disabled",
  7698. crtc->active ? "enabled" : "disabled");
  7699. crtc->base.enabled = crtc->active;
  7700. /* Because we only establish the connector -> encoder ->
  7701. * crtc links if something is active, this means the
  7702. * crtc is now deactivated. Break the links. connector
  7703. * -> encoder links are only establish when things are
  7704. * actually up, hence no need to break them. */
  7705. WARN_ON(crtc->active);
  7706. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  7707. WARN_ON(encoder->connectors_active);
  7708. encoder->base.crtc = NULL;
  7709. }
  7710. }
  7711. }
  7712. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  7713. {
  7714. struct intel_connector *connector;
  7715. struct drm_device *dev = encoder->base.dev;
  7716. /* We need to check both for a crtc link (meaning that the
  7717. * encoder is active and trying to read from a pipe) and the
  7718. * pipe itself being active. */
  7719. bool has_active_crtc = encoder->base.crtc &&
  7720. to_intel_crtc(encoder->base.crtc)->active;
  7721. if (encoder->connectors_active && !has_active_crtc) {
  7722. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  7723. encoder->base.base.id,
  7724. drm_get_encoder_name(&encoder->base));
  7725. /* Connector is active, but has no active pipe. This is
  7726. * fallout from our resume register restoring. Disable
  7727. * the encoder manually again. */
  7728. if (encoder->base.crtc) {
  7729. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  7730. encoder->base.base.id,
  7731. drm_get_encoder_name(&encoder->base));
  7732. encoder->disable(encoder);
  7733. }
  7734. /* Inconsistent output/port/pipe state happens presumably due to
  7735. * a bug in one of the get_hw_state functions. Or someplace else
  7736. * in our code, like the register restore mess on resume. Clamp
  7737. * things to off as a safer default. */
  7738. list_for_each_entry(connector,
  7739. &dev->mode_config.connector_list,
  7740. base.head) {
  7741. if (connector->encoder != encoder)
  7742. continue;
  7743. intel_connector_break_all_links(connector);
  7744. }
  7745. }
  7746. /* Enabled encoders without active connectors will be fixed in
  7747. * the crtc fixup. */
  7748. }
  7749. void i915_redisable_vga(struct drm_device *dev)
  7750. {
  7751. struct drm_i915_private *dev_priv = dev->dev_private;
  7752. u32 vga_reg = i915_vgacntrl_reg(dev);
  7753. if (I915_READ(vga_reg) != VGA_DISP_DISABLE) {
  7754. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  7755. i915_disable_vga(dev);
  7756. }
  7757. }
  7758. /* Scan out the current hw modeset state, sanitizes it and maps it into the drm
  7759. * and i915 state tracking structures. */
  7760. void intel_modeset_setup_hw_state(struct drm_device *dev,
  7761. bool force_restore)
  7762. {
  7763. struct drm_i915_private *dev_priv = dev->dev_private;
  7764. enum pipe pipe;
  7765. u32 tmp;
  7766. struct drm_plane *plane;
  7767. struct intel_crtc *crtc;
  7768. struct intel_encoder *encoder;
  7769. struct intel_connector *connector;
  7770. if (HAS_DDI(dev)) {
  7771. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7772. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7773. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7774. case TRANS_DDI_EDP_INPUT_A_ON:
  7775. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7776. pipe = PIPE_A;
  7777. break;
  7778. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7779. pipe = PIPE_B;
  7780. break;
  7781. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7782. pipe = PIPE_C;
  7783. break;
  7784. default:
  7785. /* A bogus value has been programmed, disable
  7786. * the transcoder */
  7787. WARN(1, "Bogus eDP source %08x\n", tmp);
  7788. intel_ddi_disable_transcoder_func(dev_priv,
  7789. TRANSCODER_EDP);
  7790. goto setup_pipes;
  7791. }
  7792. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  7793. crtc->config.cpu_transcoder = TRANSCODER_EDP;
  7794. DRM_DEBUG_KMS("Pipe %c using transcoder EDP\n",
  7795. pipe_name(pipe));
  7796. }
  7797. }
  7798. setup_pipes:
  7799. list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  7800. base.head) {
  7801. enum transcoder tmp = crtc->config.cpu_transcoder;
  7802. memset(&crtc->config, 0, sizeof(crtc->config));
  7803. crtc->config.cpu_transcoder = tmp;
  7804. crtc->active = dev_priv->display.get_pipe_config(crtc,
  7805. &crtc->config);
  7806. crtc->base.enabled = crtc->active;
  7807. DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
  7808. crtc->base.base.id,
  7809. crtc->active ? "enabled" : "disabled");
  7810. }
  7811. if (HAS_DDI(dev))
  7812. intel_ddi_setup_hw_pll_state(dev);
  7813. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7814. base.head) {
  7815. pipe = 0;
  7816. if (encoder->get_hw_state(encoder, &pipe)) {
  7817. encoder->base.crtc =
  7818. dev_priv->pipe_to_crtc_mapping[pipe];
  7819. } else {
  7820. encoder->base.crtc = NULL;
  7821. }
  7822. encoder->connectors_active = false;
  7823. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe=%i\n",
  7824. encoder->base.base.id,
  7825. drm_get_encoder_name(&encoder->base),
  7826. encoder->base.crtc ? "enabled" : "disabled",
  7827. pipe);
  7828. }
  7829. list_for_each_entry(connector, &dev->mode_config.connector_list,
  7830. base.head) {
  7831. if (connector->get_hw_state(connector)) {
  7832. connector->base.dpms = DRM_MODE_DPMS_ON;
  7833. connector->encoder->connectors_active = true;
  7834. connector->base.encoder = &connector->encoder->base;
  7835. } else {
  7836. connector->base.dpms = DRM_MODE_DPMS_OFF;
  7837. connector->base.encoder = NULL;
  7838. }
  7839. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  7840. connector->base.base.id,
  7841. drm_get_connector_name(&connector->base),
  7842. connector->base.encoder ? "enabled" : "disabled");
  7843. }
  7844. /* HW state is read out, now we need to sanitize this mess. */
  7845. list_for_each_entry(encoder, &dev->mode_config.encoder_list,
  7846. base.head) {
  7847. intel_sanitize_encoder(encoder);
  7848. }
  7849. for_each_pipe(pipe) {
  7850. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  7851. intel_sanitize_crtc(crtc);
  7852. }
  7853. if (force_restore) {
  7854. /*
  7855. * We need to use raw interfaces for restoring state to avoid
  7856. * checking (bogus) intermediate states.
  7857. */
  7858. for_each_pipe(pipe) {
  7859. struct drm_crtc *crtc =
  7860. dev_priv->pipe_to_crtc_mapping[pipe];
  7861. __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
  7862. crtc->fb);
  7863. }
  7864. list_for_each_entry(plane, &dev->mode_config.plane_list, head)
  7865. intel_plane_restore(plane);
  7866. i915_redisable_vga(dev);
  7867. } else {
  7868. intel_modeset_update_staged_output_state(dev);
  7869. }
  7870. intel_modeset_check_state(dev);
  7871. drm_mode_config_reset(dev);
  7872. }
  7873. void intel_modeset_gem_init(struct drm_device *dev)
  7874. {
  7875. intel_modeset_init_hw(dev);
  7876. intel_setup_overlay(dev);
  7877. intel_modeset_setup_hw_state(dev, false);
  7878. }
  7879. void intel_modeset_cleanup(struct drm_device *dev)
  7880. {
  7881. struct drm_i915_private *dev_priv = dev->dev_private;
  7882. struct drm_crtc *crtc;
  7883. struct intel_crtc *intel_crtc;
  7884. drm_kms_helper_poll_fini(dev);
  7885. mutex_lock(&dev->struct_mutex);
  7886. intel_unregister_dsm_handler();
  7887. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  7888. /* Skip inactive CRTCs */
  7889. if (!crtc->fb)
  7890. continue;
  7891. intel_crtc = to_intel_crtc(crtc);
  7892. intel_increase_pllclock(crtc);
  7893. }
  7894. intel_disable_fbc(dev);
  7895. intel_disable_gt_powersave(dev);
  7896. ironlake_teardown_rc6(dev);
  7897. if (IS_VALLEYVIEW(dev))
  7898. vlv_init_dpio(dev);
  7899. mutex_unlock(&dev->struct_mutex);
  7900. /* Disable the irq before mode object teardown, for the irq might
  7901. * enqueue unpin/hotplug work. */
  7902. drm_irq_uninstall(dev);
  7903. cancel_work_sync(&dev_priv->hotplug_work);
  7904. cancel_work_sync(&dev_priv->rps.work);
  7905. /* flush any delayed tasks or pending work */
  7906. flush_scheduled_work();
  7907. /* destroy backlight, if any, before the connectors */
  7908. intel_panel_destroy_backlight(dev);
  7909. drm_mode_config_cleanup(dev);
  7910. intel_cleanup_overlay(dev);
  7911. }
  7912. /*
  7913. * Return which encoder is currently attached for connector.
  7914. */
  7915. struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
  7916. {
  7917. return &intel_attached_encoder(connector)->base;
  7918. }
  7919. void intel_connector_attach_encoder(struct intel_connector *connector,
  7920. struct intel_encoder *encoder)
  7921. {
  7922. connector->encoder = encoder;
  7923. drm_mode_connector_attach_encoder(&connector->base,
  7924. &encoder->base);
  7925. }
  7926. /*
  7927. * set vga decode state - true == enable VGA decode
  7928. */
  7929. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  7930. {
  7931. struct drm_i915_private *dev_priv = dev->dev_private;
  7932. u16 gmch_ctrl;
  7933. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  7934. if (state)
  7935. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  7936. else
  7937. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  7938. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  7939. return 0;
  7940. }
  7941. #ifdef CONFIG_DEBUG_FS
  7942. #include <linux/seq_file.h>
  7943. struct intel_display_error_state {
  7944. struct intel_cursor_error_state {
  7945. u32 control;
  7946. u32 position;
  7947. u32 base;
  7948. u32 size;
  7949. } cursor[I915_MAX_PIPES];
  7950. struct intel_pipe_error_state {
  7951. u32 conf;
  7952. u32 source;
  7953. u32 htotal;
  7954. u32 hblank;
  7955. u32 hsync;
  7956. u32 vtotal;
  7957. u32 vblank;
  7958. u32 vsync;
  7959. } pipe[I915_MAX_PIPES];
  7960. struct intel_plane_error_state {
  7961. u32 control;
  7962. u32 stride;
  7963. u32 size;
  7964. u32 pos;
  7965. u32 addr;
  7966. u32 surface;
  7967. u32 tile_offset;
  7968. } plane[I915_MAX_PIPES];
  7969. };
  7970. struct intel_display_error_state *
  7971. intel_display_capture_error_state(struct drm_device *dev)
  7972. {
  7973. drm_i915_private_t *dev_priv = dev->dev_private;
  7974. struct intel_display_error_state *error;
  7975. enum transcoder cpu_transcoder;
  7976. int i;
  7977. error = kmalloc(sizeof(*error), GFP_ATOMIC);
  7978. if (error == NULL)
  7979. return NULL;
  7980. for_each_pipe(i) {
  7981. cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv, i);
  7982. if (INTEL_INFO(dev)->gen <= 6 || IS_VALLEYVIEW(dev)) {
  7983. error->cursor[i].control = I915_READ(CURCNTR(i));
  7984. error->cursor[i].position = I915_READ(CURPOS(i));
  7985. error->cursor[i].base = I915_READ(CURBASE(i));
  7986. } else {
  7987. error->cursor[i].control = I915_READ(CURCNTR_IVB(i));
  7988. error->cursor[i].position = I915_READ(CURPOS_IVB(i));
  7989. error->cursor[i].base = I915_READ(CURBASE_IVB(i));
  7990. }
  7991. error->plane[i].control = I915_READ(DSPCNTR(i));
  7992. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  7993. if (INTEL_INFO(dev)->gen <= 3) {
  7994. error->plane[i].size = I915_READ(DSPSIZE(i));
  7995. error->plane[i].pos = I915_READ(DSPPOS(i));
  7996. }
  7997. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  7998. error->plane[i].addr = I915_READ(DSPADDR(i));
  7999. if (INTEL_INFO(dev)->gen >= 4) {
  8000. error->plane[i].surface = I915_READ(DSPSURF(i));
  8001. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  8002. }
  8003. error->pipe[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  8004. error->pipe[i].source = I915_READ(PIPESRC(i));
  8005. error->pipe[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  8006. error->pipe[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  8007. error->pipe[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  8008. error->pipe[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  8009. error->pipe[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  8010. error->pipe[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  8011. }
  8012. return error;
  8013. }
  8014. void
  8015. intel_display_print_error_state(struct seq_file *m,
  8016. struct drm_device *dev,
  8017. struct intel_display_error_state *error)
  8018. {
  8019. int i;
  8020. seq_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  8021. for_each_pipe(i) {
  8022. seq_printf(m, "Pipe [%d]:\n", i);
  8023. seq_printf(m, " CONF: %08x\n", error->pipe[i].conf);
  8024. seq_printf(m, " SRC: %08x\n", error->pipe[i].source);
  8025. seq_printf(m, " HTOTAL: %08x\n", error->pipe[i].htotal);
  8026. seq_printf(m, " HBLANK: %08x\n", error->pipe[i].hblank);
  8027. seq_printf(m, " HSYNC: %08x\n", error->pipe[i].hsync);
  8028. seq_printf(m, " VTOTAL: %08x\n", error->pipe[i].vtotal);
  8029. seq_printf(m, " VBLANK: %08x\n", error->pipe[i].vblank);
  8030. seq_printf(m, " VSYNC: %08x\n", error->pipe[i].vsync);
  8031. seq_printf(m, "Plane [%d]:\n", i);
  8032. seq_printf(m, " CNTR: %08x\n", error->plane[i].control);
  8033. seq_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  8034. if (INTEL_INFO(dev)->gen <= 3) {
  8035. seq_printf(m, " SIZE: %08x\n", error->plane[i].size);
  8036. seq_printf(m, " POS: %08x\n", error->plane[i].pos);
  8037. }
  8038. if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
  8039. seq_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  8040. if (INTEL_INFO(dev)->gen >= 4) {
  8041. seq_printf(m, " SURF: %08x\n", error->plane[i].surface);
  8042. seq_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  8043. }
  8044. seq_printf(m, "Cursor [%d]:\n", i);
  8045. seq_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  8046. seq_printf(m, " POS: %08x\n", error->cursor[i].position);
  8047. seq_printf(m, " BASE: %08x\n", error->cursor[i].base);
  8048. }
  8049. }
  8050. #endif