intel_display.c 227 KB

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