intel_display.c 253 KB

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