intel_display.c 303 KB

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