intel_display.c 256 KB

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