intel_display.c 167 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839
  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/module.h>
  27. #include <linux/input.h>
  28. #include <linux/i2c.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include "drmP.h"
  32. #include "intel_drv.h"
  33. #include "i915_drm.h"
  34. #include "i915_drv.h"
  35. #include "drm_dp_helper.h"
  36. #include "drm_crtc_helper.h"
  37. #define HAS_eDP (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
  38. bool intel_pipe_has_type (struct drm_crtc *crtc, int type);
  39. static void intel_update_watermarks(struct drm_device *dev);
  40. static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule);
  41. typedef struct {
  42. /* given values */
  43. int n;
  44. int m1, m2;
  45. int p1, p2;
  46. /* derived values */
  47. int dot;
  48. int vco;
  49. int m;
  50. int p;
  51. } intel_clock_t;
  52. typedef struct {
  53. int min, max;
  54. } intel_range_t;
  55. typedef struct {
  56. int dot_limit;
  57. int p2_slow, p2_fast;
  58. } intel_p2_t;
  59. #define INTEL_P2_NUM 2
  60. typedef struct intel_limit intel_limit_t;
  61. struct intel_limit {
  62. intel_range_t dot, vco, n, m, m1, m2, p, p1;
  63. intel_p2_t p2;
  64. bool (* find_pll)(const intel_limit_t *, struct drm_crtc *,
  65. int, int, intel_clock_t *);
  66. };
  67. #define I8XX_DOT_MIN 25000
  68. #define I8XX_DOT_MAX 350000
  69. #define I8XX_VCO_MIN 930000
  70. #define I8XX_VCO_MAX 1400000
  71. #define I8XX_N_MIN 3
  72. #define I8XX_N_MAX 16
  73. #define I8XX_M_MIN 96
  74. #define I8XX_M_MAX 140
  75. #define I8XX_M1_MIN 18
  76. #define I8XX_M1_MAX 26
  77. #define I8XX_M2_MIN 6
  78. #define I8XX_M2_MAX 16
  79. #define I8XX_P_MIN 4
  80. #define I8XX_P_MAX 128
  81. #define I8XX_P1_MIN 2
  82. #define I8XX_P1_MAX 33
  83. #define I8XX_P1_LVDS_MIN 1
  84. #define I8XX_P1_LVDS_MAX 6
  85. #define I8XX_P2_SLOW 4
  86. #define I8XX_P2_FAST 2
  87. #define I8XX_P2_LVDS_SLOW 14
  88. #define I8XX_P2_LVDS_FAST 7
  89. #define I8XX_P2_SLOW_LIMIT 165000
  90. #define I9XX_DOT_MIN 20000
  91. #define I9XX_DOT_MAX 400000
  92. #define I9XX_VCO_MIN 1400000
  93. #define I9XX_VCO_MAX 2800000
  94. #define PINEVIEW_VCO_MIN 1700000
  95. #define PINEVIEW_VCO_MAX 3500000
  96. #define I9XX_N_MIN 1
  97. #define I9XX_N_MAX 6
  98. /* Pineview's Ncounter is a ring counter */
  99. #define PINEVIEW_N_MIN 3
  100. #define PINEVIEW_N_MAX 6
  101. #define I9XX_M_MIN 70
  102. #define I9XX_M_MAX 120
  103. #define PINEVIEW_M_MIN 2
  104. #define PINEVIEW_M_MAX 256
  105. #define I9XX_M1_MIN 10
  106. #define I9XX_M1_MAX 22
  107. #define I9XX_M2_MIN 5
  108. #define I9XX_M2_MAX 9
  109. /* Pineview M1 is reserved, and must be 0 */
  110. #define PINEVIEW_M1_MIN 0
  111. #define PINEVIEW_M1_MAX 0
  112. #define PINEVIEW_M2_MIN 0
  113. #define PINEVIEW_M2_MAX 254
  114. #define I9XX_P_SDVO_DAC_MIN 5
  115. #define I9XX_P_SDVO_DAC_MAX 80
  116. #define I9XX_P_LVDS_MIN 7
  117. #define I9XX_P_LVDS_MAX 98
  118. #define PINEVIEW_P_LVDS_MIN 7
  119. #define PINEVIEW_P_LVDS_MAX 112
  120. #define I9XX_P1_MIN 1
  121. #define I9XX_P1_MAX 8
  122. #define I9XX_P2_SDVO_DAC_SLOW 10
  123. #define I9XX_P2_SDVO_DAC_FAST 5
  124. #define I9XX_P2_SDVO_DAC_SLOW_LIMIT 200000
  125. #define I9XX_P2_LVDS_SLOW 14
  126. #define I9XX_P2_LVDS_FAST 7
  127. #define I9XX_P2_LVDS_SLOW_LIMIT 112000
  128. /*The parameter is for SDVO on G4x platform*/
  129. #define G4X_DOT_SDVO_MIN 25000
  130. #define G4X_DOT_SDVO_MAX 270000
  131. #define G4X_VCO_MIN 1750000
  132. #define G4X_VCO_MAX 3500000
  133. #define G4X_N_SDVO_MIN 1
  134. #define G4X_N_SDVO_MAX 4
  135. #define G4X_M_SDVO_MIN 104
  136. #define G4X_M_SDVO_MAX 138
  137. #define G4X_M1_SDVO_MIN 17
  138. #define G4X_M1_SDVO_MAX 23
  139. #define G4X_M2_SDVO_MIN 5
  140. #define G4X_M2_SDVO_MAX 11
  141. #define G4X_P_SDVO_MIN 10
  142. #define G4X_P_SDVO_MAX 30
  143. #define G4X_P1_SDVO_MIN 1
  144. #define G4X_P1_SDVO_MAX 3
  145. #define G4X_P2_SDVO_SLOW 10
  146. #define G4X_P2_SDVO_FAST 10
  147. #define G4X_P2_SDVO_LIMIT 270000
  148. /*The parameter is for HDMI_DAC on G4x platform*/
  149. #define G4X_DOT_HDMI_DAC_MIN 22000
  150. #define G4X_DOT_HDMI_DAC_MAX 400000
  151. #define G4X_N_HDMI_DAC_MIN 1
  152. #define G4X_N_HDMI_DAC_MAX 4
  153. #define G4X_M_HDMI_DAC_MIN 104
  154. #define G4X_M_HDMI_DAC_MAX 138
  155. #define G4X_M1_HDMI_DAC_MIN 16
  156. #define G4X_M1_HDMI_DAC_MAX 23
  157. #define G4X_M2_HDMI_DAC_MIN 5
  158. #define G4X_M2_HDMI_DAC_MAX 11
  159. #define G4X_P_HDMI_DAC_MIN 5
  160. #define G4X_P_HDMI_DAC_MAX 80
  161. #define G4X_P1_HDMI_DAC_MIN 1
  162. #define G4X_P1_HDMI_DAC_MAX 8
  163. #define G4X_P2_HDMI_DAC_SLOW 10
  164. #define G4X_P2_HDMI_DAC_FAST 5
  165. #define G4X_P2_HDMI_DAC_LIMIT 165000
  166. /*The parameter is for SINGLE_CHANNEL_LVDS on G4x platform*/
  167. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MIN 20000
  168. #define G4X_DOT_SINGLE_CHANNEL_LVDS_MAX 115000
  169. #define G4X_N_SINGLE_CHANNEL_LVDS_MIN 1
  170. #define G4X_N_SINGLE_CHANNEL_LVDS_MAX 3
  171. #define G4X_M_SINGLE_CHANNEL_LVDS_MIN 104
  172. #define G4X_M_SINGLE_CHANNEL_LVDS_MAX 138
  173. #define G4X_M1_SINGLE_CHANNEL_LVDS_MIN 17
  174. #define G4X_M1_SINGLE_CHANNEL_LVDS_MAX 23
  175. #define G4X_M2_SINGLE_CHANNEL_LVDS_MIN 5
  176. #define G4X_M2_SINGLE_CHANNEL_LVDS_MAX 11
  177. #define G4X_P_SINGLE_CHANNEL_LVDS_MIN 28
  178. #define G4X_P_SINGLE_CHANNEL_LVDS_MAX 112
  179. #define G4X_P1_SINGLE_CHANNEL_LVDS_MIN 2
  180. #define G4X_P1_SINGLE_CHANNEL_LVDS_MAX 8
  181. #define G4X_P2_SINGLE_CHANNEL_LVDS_SLOW 14
  182. #define G4X_P2_SINGLE_CHANNEL_LVDS_FAST 14
  183. #define G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT 0
  184. /*The parameter is for DUAL_CHANNEL_LVDS on G4x platform*/
  185. #define G4X_DOT_DUAL_CHANNEL_LVDS_MIN 80000
  186. #define G4X_DOT_DUAL_CHANNEL_LVDS_MAX 224000
  187. #define G4X_N_DUAL_CHANNEL_LVDS_MIN 1
  188. #define G4X_N_DUAL_CHANNEL_LVDS_MAX 3
  189. #define G4X_M_DUAL_CHANNEL_LVDS_MIN 104
  190. #define G4X_M_DUAL_CHANNEL_LVDS_MAX 138
  191. #define G4X_M1_DUAL_CHANNEL_LVDS_MIN 17
  192. #define G4X_M1_DUAL_CHANNEL_LVDS_MAX 23
  193. #define G4X_M2_DUAL_CHANNEL_LVDS_MIN 5
  194. #define G4X_M2_DUAL_CHANNEL_LVDS_MAX 11
  195. #define G4X_P_DUAL_CHANNEL_LVDS_MIN 14
  196. #define G4X_P_DUAL_CHANNEL_LVDS_MAX 42
  197. #define G4X_P1_DUAL_CHANNEL_LVDS_MIN 2
  198. #define G4X_P1_DUAL_CHANNEL_LVDS_MAX 6
  199. #define G4X_P2_DUAL_CHANNEL_LVDS_SLOW 7
  200. #define G4X_P2_DUAL_CHANNEL_LVDS_FAST 7
  201. #define G4X_P2_DUAL_CHANNEL_LVDS_LIMIT 0
  202. /*The parameter is for DISPLAY PORT on G4x platform*/
  203. #define G4X_DOT_DISPLAY_PORT_MIN 161670
  204. #define G4X_DOT_DISPLAY_PORT_MAX 227000
  205. #define G4X_N_DISPLAY_PORT_MIN 1
  206. #define G4X_N_DISPLAY_PORT_MAX 2
  207. #define G4X_M_DISPLAY_PORT_MIN 97
  208. #define G4X_M_DISPLAY_PORT_MAX 108
  209. #define G4X_M1_DISPLAY_PORT_MIN 0x10
  210. #define G4X_M1_DISPLAY_PORT_MAX 0x12
  211. #define G4X_M2_DISPLAY_PORT_MIN 0x05
  212. #define G4X_M2_DISPLAY_PORT_MAX 0x06
  213. #define G4X_P_DISPLAY_PORT_MIN 10
  214. #define G4X_P_DISPLAY_PORT_MAX 20
  215. #define G4X_P1_DISPLAY_PORT_MIN 1
  216. #define G4X_P1_DISPLAY_PORT_MAX 2
  217. #define G4X_P2_DISPLAY_PORT_SLOW 10
  218. #define G4X_P2_DISPLAY_PORT_FAST 10
  219. #define G4X_P2_DISPLAY_PORT_LIMIT 0
  220. /* Ironlake / Sandybridge */
  221. /* as we calculate clock using (register_value + 2) for
  222. N/M1/M2, so here the range value for them is (actual_value-2).
  223. */
  224. #define IRONLAKE_DOT_MIN 25000
  225. #define IRONLAKE_DOT_MAX 350000
  226. #define IRONLAKE_VCO_MIN 1760000
  227. #define IRONLAKE_VCO_MAX 3510000
  228. #define IRONLAKE_M1_MIN 12
  229. #define IRONLAKE_M1_MAX 22
  230. #define IRONLAKE_M2_MIN 5
  231. #define IRONLAKE_M2_MAX 9
  232. #define IRONLAKE_P2_DOT_LIMIT 225000 /* 225Mhz */
  233. /* We have parameter ranges for different type of outputs. */
  234. /* DAC & HDMI Refclk 120Mhz */
  235. #define IRONLAKE_DAC_N_MIN 1
  236. #define IRONLAKE_DAC_N_MAX 5
  237. #define IRONLAKE_DAC_M_MIN 79
  238. #define IRONLAKE_DAC_M_MAX 127
  239. #define IRONLAKE_DAC_P_MIN 5
  240. #define IRONLAKE_DAC_P_MAX 80
  241. #define IRONLAKE_DAC_P1_MIN 1
  242. #define IRONLAKE_DAC_P1_MAX 8
  243. #define IRONLAKE_DAC_P2_SLOW 10
  244. #define IRONLAKE_DAC_P2_FAST 5
  245. /* LVDS single-channel 120Mhz refclk */
  246. #define IRONLAKE_LVDS_S_N_MIN 1
  247. #define IRONLAKE_LVDS_S_N_MAX 3
  248. #define IRONLAKE_LVDS_S_M_MIN 79
  249. #define IRONLAKE_LVDS_S_M_MAX 118
  250. #define IRONLAKE_LVDS_S_P_MIN 28
  251. #define IRONLAKE_LVDS_S_P_MAX 112
  252. #define IRONLAKE_LVDS_S_P1_MIN 2
  253. #define IRONLAKE_LVDS_S_P1_MAX 8
  254. #define IRONLAKE_LVDS_S_P2_SLOW 14
  255. #define IRONLAKE_LVDS_S_P2_FAST 14
  256. /* LVDS dual-channel 120Mhz refclk */
  257. #define IRONLAKE_LVDS_D_N_MIN 1
  258. #define IRONLAKE_LVDS_D_N_MAX 3
  259. #define IRONLAKE_LVDS_D_M_MIN 79
  260. #define IRONLAKE_LVDS_D_M_MAX 127
  261. #define IRONLAKE_LVDS_D_P_MIN 14
  262. #define IRONLAKE_LVDS_D_P_MAX 56
  263. #define IRONLAKE_LVDS_D_P1_MIN 2
  264. #define IRONLAKE_LVDS_D_P1_MAX 8
  265. #define IRONLAKE_LVDS_D_P2_SLOW 7
  266. #define IRONLAKE_LVDS_D_P2_FAST 7
  267. /* LVDS single-channel 100Mhz refclk */
  268. #define IRONLAKE_LVDS_S_SSC_N_MIN 1
  269. #define IRONLAKE_LVDS_S_SSC_N_MAX 2
  270. #define IRONLAKE_LVDS_S_SSC_M_MIN 79
  271. #define IRONLAKE_LVDS_S_SSC_M_MAX 126
  272. #define IRONLAKE_LVDS_S_SSC_P_MIN 28
  273. #define IRONLAKE_LVDS_S_SSC_P_MAX 112
  274. #define IRONLAKE_LVDS_S_SSC_P1_MIN 2
  275. #define IRONLAKE_LVDS_S_SSC_P1_MAX 8
  276. #define IRONLAKE_LVDS_S_SSC_P2_SLOW 14
  277. #define IRONLAKE_LVDS_S_SSC_P2_FAST 14
  278. /* LVDS dual-channel 100Mhz refclk */
  279. #define IRONLAKE_LVDS_D_SSC_N_MIN 1
  280. #define IRONLAKE_LVDS_D_SSC_N_MAX 3
  281. #define IRONLAKE_LVDS_D_SSC_M_MIN 79
  282. #define IRONLAKE_LVDS_D_SSC_M_MAX 126
  283. #define IRONLAKE_LVDS_D_SSC_P_MIN 14
  284. #define IRONLAKE_LVDS_D_SSC_P_MAX 42
  285. #define IRONLAKE_LVDS_D_SSC_P1_MIN 2
  286. #define IRONLAKE_LVDS_D_SSC_P1_MAX 6
  287. #define IRONLAKE_LVDS_D_SSC_P2_SLOW 7
  288. #define IRONLAKE_LVDS_D_SSC_P2_FAST 7
  289. /* DisplayPort */
  290. #define IRONLAKE_DP_N_MIN 1
  291. #define IRONLAKE_DP_N_MAX 2
  292. #define IRONLAKE_DP_M_MIN 81
  293. #define IRONLAKE_DP_M_MAX 90
  294. #define IRONLAKE_DP_P_MIN 10
  295. #define IRONLAKE_DP_P_MAX 20
  296. #define IRONLAKE_DP_P2_FAST 10
  297. #define IRONLAKE_DP_P2_SLOW 10
  298. #define IRONLAKE_DP_P2_LIMIT 0
  299. #define IRONLAKE_DP_P1_MIN 1
  300. #define IRONLAKE_DP_P1_MAX 2
  301. static bool
  302. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  303. int target, int refclk, intel_clock_t *best_clock);
  304. static bool
  305. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  306. int target, int refclk, intel_clock_t *best_clock);
  307. static bool
  308. intel_find_pll_g4x_dp(const intel_limit_t *, struct drm_crtc *crtc,
  309. int target, int refclk, intel_clock_t *best_clock);
  310. static bool
  311. intel_find_pll_ironlake_dp(const intel_limit_t *, struct drm_crtc *crtc,
  312. int target, int refclk, intel_clock_t *best_clock);
  313. static const intel_limit_t intel_limits_i8xx_dvo = {
  314. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  315. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  316. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  317. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  318. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  319. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  320. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  321. .p1 = { .min = I8XX_P1_MIN, .max = I8XX_P1_MAX },
  322. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  323. .p2_slow = I8XX_P2_SLOW, .p2_fast = I8XX_P2_FAST },
  324. .find_pll = intel_find_best_PLL,
  325. };
  326. static const intel_limit_t intel_limits_i8xx_lvds = {
  327. .dot = { .min = I8XX_DOT_MIN, .max = I8XX_DOT_MAX },
  328. .vco = { .min = I8XX_VCO_MIN, .max = I8XX_VCO_MAX },
  329. .n = { .min = I8XX_N_MIN, .max = I8XX_N_MAX },
  330. .m = { .min = I8XX_M_MIN, .max = I8XX_M_MAX },
  331. .m1 = { .min = I8XX_M1_MIN, .max = I8XX_M1_MAX },
  332. .m2 = { .min = I8XX_M2_MIN, .max = I8XX_M2_MAX },
  333. .p = { .min = I8XX_P_MIN, .max = I8XX_P_MAX },
  334. .p1 = { .min = I8XX_P1_LVDS_MIN, .max = I8XX_P1_LVDS_MAX },
  335. .p2 = { .dot_limit = I8XX_P2_SLOW_LIMIT,
  336. .p2_slow = I8XX_P2_LVDS_SLOW, .p2_fast = I8XX_P2_LVDS_FAST },
  337. .find_pll = intel_find_best_PLL,
  338. };
  339. static const intel_limit_t intel_limits_i9xx_sdvo = {
  340. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  341. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  342. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  343. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  344. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  345. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  346. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  347. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  348. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  349. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  350. .find_pll = intel_find_best_PLL,
  351. };
  352. static const intel_limit_t intel_limits_i9xx_lvds = {
  353. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  354. .vco = { .min = I9XX_VCO_MIN, .max = I9XX_VCO_MAX },
  355. .n = { .min = I9XX_N_MIN, .max = I9XX_N_MAX },
  356. .m = { .min = I9XX_M_MIN, .max = I9XX_M_MAX },
  357. .m1 = { .min = I9XX_M1_MIN, .max = I9XX_M1_MAX },
  358. .m2 = { .min = I9XX_M2_MIN, .max = I9XX_M2_MAX },
  359. .p = { .min = I9XX_P_LVDS_MIN, .max = I9XX_P_LVDS_MAX },
  360. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  361. /* The single-channel range is 25-112Mhz, and dual-channel
  362. * is 80-224Mhz. Prefer single channel as much as possible.
  363. */
  364. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  365. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_FAST },
  366. .find_pll = intel_find_best_PLL,
  367. };
  368. /* below parameter and function is for G4X Chipset Family*/
  369. static const intel_limit_t intel_limits_g4x_sdvo = {
  370. .dot = { .min = G4X_DOT_SDVO_MIN, .max = G4X_DOT_SDVO_MAX },
  371. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  372. .n = { .min = G4X_N_SDVO_MIN, .max = G4X_N_SDVO_MAX },
  373. .m = { .min = G4X_M_SDVO_MIN, .max = G4X_M_SDVO_MAX },
  374. .m1 = { .min = G4X_M1_SDVO_MIN, .max = G4X_M1_SDVO_MAX },
  375. .m2 = { .min = G4X_M2_SDVO_MIN, .max = G4X_M2_SDVO_MAX },
  376. .p = { .min = G4X_P_SDVO_MIN, .max = G4X_P_SDVO_MAX },
  377. .p1 = { .min = G4X_P1_SDVO_MIN, .max = G4X_P1_SDVO_MAX},
  378. .p2 = { .dot_limit = G4X_P2_SDVO_LIMIT,
  379. .p2_slow = G4X_P2_SDVO_SLOW,
  380. .p2_fast = G4X_P2_SDVO_FAST
  381. },
  382. .find_pll = intel_g4x_find_best_PLL,
  383. };
  384. static const intel_limit_t intel_limits_g4x_hdmi = {
  385. .dot = { .min = G4X_DOT_HDMI_DAC_MIN, .max = G4X_DOT_HDMI_DAC_MAX },
  386. .vco = { .min = G4X_VCO_MIN, .max = G4X_VCO_MAX},
  387. .n = { .min = G4X_N_HDMI_DAC_MIN, .max = G4X_N_HDMI_DAC_MAX },
  388. .m = { .min = G4X_M_HDMI_DAC_MIN, .max = G4X_M_HDMI_DAC_MAX },
  389. .m1 = { .min = G4X_M1_HDMI_DAC_MIN, .max = G4X_M1_HDMI_DAC_MAX },
  390. .m2 = { .min = G4X_M2_HDMI_DAC_MIN, .max = G4X_M2_HDMI_DAC_MAX },
  391. .p = { .min = G4X_P_HDMI_DAC_MIN, .max = G4X_P_HDMI_DAC_MAX },
  392. .p1 = { .min = G4X_P1_HDMI_DAC_MIN, .max = G4X_P1_HDMI_DAC_MAX},
  393. .p2 = { .dot_limit = G4X_P2_HDMI_DAC_LIMIT,
  394. .p2_slow = G4X_P2_HDMI_DAC_SLOW,
  395. .p2_fast = G4X_P2_HDMI_DAC_FAST
  396. },
  397. .find_pll = intel_g4x_find_best_PLL,
  398. };
  399. static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
  400. .dot = { .min = G4X_DOT_SINGLE_CHANNEL_LVDS_MIN,
  401. .max = G4X_DOT_SINGLE_CHANNEL_LVDS_MAX },
  402. .vco = { .min = G4X_VCO_MIN,
  403. .max = G4X_VCO_MAX },
  404. .n = { .min = G4X_N_SINGLE_CHANNEL_LVDS_MIN,
  405. .max = G4X_N_SINGLE_CHANNEL_LVDS_MAX },
  406. .m = { .min = G4X_M_SINGLE_CHANNEL_LVDS_MIN,
  407. .max = G4X_M_SINGLE_CHANNEL_LVDS_MAX },
  408. .m1 = { .min = G4X_M1_SINGLE_CHANNEL_LVDS_MIN,
  409. .max = G4X_M1_SINGLE_CHANNEL_LVDS_MAX },
  410. .m2 = { .min = G4X_M2_SINGLE_CHANNEL_LVDS_MIN,
  411. .max = G4X_M2_SINGLE_CHANNEL_LVDS_MAX },
  412. .p = { .min = G4X_P_SINGLE_CHANNEL_LVDS_MIN,
  413. .max = G4X_P_SINGLE_CHANNEL_LVDS_MAX },
  414. .p1 = { .min = G4X_P1_SINGLE_CHANNEL_LVDS_MIN,
  415. .max = G4X_P1_SINGLE_CHANNEL_LVDS_MAX },
  416. .p2 = { .dot_limit = G4X_P2_SINGLE_CHANNEL_LVDS_LIMIT,
  417. .p2_slow = G4X_P2_SINGLE_CHANNEL_LVDS_SLOW,
  418. .p2_fast = G4X_P2_SINGLE_CHANNEL_LVDS_FAST
  419. },
  420. .find_pll = intel_g4x_find_best_PLL,
  421. };
  422. static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
  423. .dot = { .min = G4X_DOT_DUAL_CHANNEL_LVDS_MIN,
  424. .max = G4X_DOT_DUAL_CHANNEL_LVDS_MAX },
  425. .vco = { .min = G4X_VCO_MIN,
  426. .max = G4X_VCO_MAX },
  427. .n = { .min = G4X_N_DUAL_CHANNEL_LVDS_MIN,
  428. .max = G4X_N_DUAL_CHANNEL_LVDS_MAX },
  429. .m = { .min = G4X_M_DUAL_CHANNEL_LVDS_MIN,
  430. .max = G4X_M_DUAL_CHANNEL_LVDS_MAX },
  431. .m1 = { .min = G4X_M1_DUAL_CHANNEL_LVDS_MIN,
  432. .max = G4X_M1_DUAL_CHANNEL_LVDS_MAX },
  433. .m2 = { .min = G4X_M2_DUAL_CHANNEL_LVDS_MIN,
  434. .max = G4X_M2_DUAL_CHANNEL_LVDS_MAX },
  435. .p = { .min = G4X_P_DUAL_CHANNEL_LVDS_MIN,
  436. .max = G4X_P_DUAL_CHANNEL_LVDS_MAX },
  437. .p1 = { .min = G4X_P1_DUAL_CHANNEL_LVDS_MIN,
  438. .max = G4X_P1_DUAL_CHANNEL_LVDS_MAX },
  439. .p2 = { .dot_limit = G4X_P2_DUAL_CHANNEL_LVDS_LIMIT,
  440. .p2_slow = G4X_P2_DUAL_CHANNEL_LVDS_SLOW,
  441. .p2_fast = G4X_P2_DUAL_CHANNEL_LVDS_FAST
  442. },
  443. .find_pll = intel_g4x_find_best_PLL,
  444. };
  445. static const intel_limit_t intel_limits_g4x_display_port = {
  446. .dot = { .min = G4X_DOT_DISPLAY_PORT_MIN,
  447. .max = G4X_DOT_DISPLAY_PORT_MAX },
  448. .vco = { .min = G4X_VCO_MIN,
  449. .max = G4X_VCO_MAX},
  450. .n = { .min = G4X_N_DISPLAY_PORT_MIN,
  451. .max = G4X_N_DISPLAY_PORT_MAX },
  452. .m = { .min = G4X_M_DISPLAY_PORT_MIN,
  453. .max = G4X_M_DISPLAY_PORT_MAX },
  454. .m1 = { .min = G4X_M1_DISPLAY_PORT_MIN,
  455. .max = G4X_M1_DISPLAY_PORT_MAX },
  456. .m2 = { .min = G4X_M2_DISPLAY_PORT_MIN,
  457. .max = G4X_M2_DISPLAY_PORT_MAX },
  458. .p = { .min = G4X_P_DISPLAY_PORT_MIN,
  459. .max = G4X_P_DISPLAY_PORT_MAX },
  460. .p1 = { .min = G4X_P1_DISPLAY_PORT_MIN,
  461. .max = G4X_P1_DISPLAY_PORT_MAX},
  462. .p2 = { .dot_limit = G4X_P2_DISPLAY_PORT_LIMIT,
  463. .p2_slow = G4X_P2_DISPLAY_PORT_SLOW,
  464. .p2_fast = G4X_P2_DISPLAY_PORT_FAST },
  465. .find_pll = intel_find_pll_g4x_dp,
  466. };
  467. static const intel_limit_t intel_limits_pineview_sdvo = {
  468. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX},
  469. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  470. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  471. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  472. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  473. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  474. .p = { .min = I9XX_P_SDVO_DAC_MIN, .max = I9XX_P_SDVO_DAC_MAX },
  475. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  476. .p2 = { .dot_limit = I9XX_P2_SDVO_DAC_SLOW_LIMIT,
  477. .p2_slow = I9XX_P2_SDVO_DAC_SLOW, .p2_fast = I9XX_P2_SDVO_DAC_FAST },
  478. .find_pll = intel_find_best_PLL,
  479. };
  480. static const intel_limit_t intel_limits_pineview_lvds = {
  481. .dot = { .min = I9XX_DOT_MIN, .max = I9XX_DOT_MAX },
  482. .vco = { .min = PINEVIEW_VCO_MIN, .max = PINEVIEW_VCO_MAX },
  483. .n = { .min = PINEVIEW_N_MIN, .max = PINEVIEW_N_MAX },
  484. .m = { .min = PINEVIEW_M_MIN, .max = PINEVIEW_M_MAX },
  485. .m1 = { .min = PINEVIEW_M1_MIN, .max = PINEVIEW_M1_MAX },
  486. .m2 = { .min = PINEVIEW_M2_MIN, .max = PINEVIEW_M2_MAX },
  487. .p = { .min = PINEVIEW_P_LVDS_MIN, .max = PINEVIEW_P_LVDS_MAX },
  488. .p1 = { .min = I9XX_P1_MIN, .max = I9XX_P1_MAX },
  489. /* Pineview only supports single-channel mode. */
  490. .p2 = { .dot_limit = I9XX_P2_LVDS_SLOW_LIMIT,
  491. .p2_slow = I9XX_P2_LVDS_SLOW, .p2_fast = I9XX_P2_LVDS_SLOW },
  492. .find_pll = intel_find_best_PLL,
  493. };
  494. static const intel_limit_t intel_limits_ironlake_dac = {
  495. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  496. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  497. .n = { .min = IRONLAKE_DAC_N_MIN, .max = IRONLAKE_DAC_N_MAX },
  498. .m = { .min = IRONLAKE_DAC_M_MIN, .max = IRONLAKE_DAC_M_MAX },
  499. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  500. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  501. .p = { .min = IRONLAKE_DAC_P_MIN, .max = IRONLAKE_DAC_P_MAX },
  502. .p1 = { .min = IRONLAKE_DAC_P1_MIN, .max = IRONLAKE_DAC_P1_MAX },
  503. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  504. .p2_slow = IRONLAKE_DAC_P2_SLOW,
  505. .p2_fast = IRONLAKE_DAC_P2_FAST },
  506. .find_pll = intel_g4x_find_best_PLL,
  507. };
  508. static const intel_limit_t intel_limits_ironlake_single_lvds = {
  509. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  510. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  511. .n = { .min = IRONLAKE_LVDS_S_N_MIN, .max = IRONLAKE_LVDS_S_N_MAX },
  512. .m = { .min = IRONLAKE_LVDS_S_M_MIN, .max = IRONLAKE_LVDS_S_M_MAX },
  513. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  514. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  515. .p = { .min = IRONLAKE_LVDS_S_P_MIN, .max = IRONLAKE_LVDS_S_P_MAX },
  516. .p1 = { .min = IRONLAKE_LVDS_S_P1_MIN, .max = IRONLAKE_LVDS_S_P1_MAX },
  517. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  518. .p2_slow = IRONLAKE_LVDS_S_P2_SLOW,
  519. .p2_fast = IRONLAKE_LVDS_S_P2_FAST },
  520. .find_pll = intel_g4x_find_best_PLL,
  521. };
  522. static const intel_limit_t intel_limits_ironlake_dual_lvds = {
  523. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  524. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  525. .n = { .min = IRONLAKE_LVDS_D_N_MIN, .max = IRONLAKE_LVDS_D_N_MAX },
  526. .m = { .min = IRONLAKE_LVDS_D_M_MIN, .max = IRONLAKE_LVDS_D_M_MAX },
  527. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  528. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  529. .p = { .min = IRONLAKE_LVDS_D_P_MIN, .max = IRONLAKE_LVDS_D_P_MAX },
  530. .p1 = { .min = IRONLAKE_LVDS_D_P1_MIN, .max = IRONLAKE_LVDS_D_P1_MAX },
  531. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  532. .p2_slow = IRONLAKE_LVDS_D_P2_SLOW,
  533. .p2_fast = IRONLAKE_LVDS_D_P2_FAST },
  534. .find_pll = intel_g4x_find_best_PLL,
  535. };
  536. static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
  537. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  538. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  539. .n = { .min = IRONLAKE_LVDS_S_SSC_N_MIN, .max = IRONLAKE_LVDS_S_SSC_N_MAX },
  540. .m = { .min = IRONLAKE_LVDS_S_SSC_M_MIN, .max = IRONLAKE_LVDS_S_SSC_M_MAX },
  541. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  542. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  543. .p = { .min = IRONLAKE_LVDS_S_SSC_P_MIN, .max = IRONLAKE_LVDS_S_SSC_P_MAX },
  544. .p1 = { .min = IRONLAKE_LVDS_S_SSC_P1_MIN,.max = IRONLAKE_LVDS_S_SSC_P1_MAX },
  545. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  546. .p2_slow = IRONLAKE_LVDS_S_SSC_P2_SLOW,
  547. .p2_fast = IRONLAKE_LVDS_S_SSC_P2_FAST },
  548. .find_pll = intel_g4x_find_best_PLL,
  549. };
  550. static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
  551. .dot = { .min = IRONLAKE_DOT_MIN, .max = IRONLAKE_DOT_MAX },
  552. .vco = { .min = IRONLAKE_VCO_MIN, .max = IRONLAKE_VCO_MAX },
  553. .n = { .min = IRONLAKE_LVDS_D_SSC_N_MIN, .max = IRONLAKE_LVDS_D_SSC_N_MAX },
  554. .m = { .min = IRONLAKE_LVDS_D_SSC_M_MIN, .max = IRONLAKE_LVDS_D_SSC_M_MAX },
  555. .m1 = { .min = IRONLAKE_M1_MIN, .max = IRONLAKE_M1_MAX },
  556. .m2 = { .min = IRONLAKE_M2_MIN, .max = IRONLAKE_M2_MAX },
  557. .p = { .min = IRONLAKE_LVDS_D_SSC_P_MIN, .max = IRONLAKE_LVDS_D_SSC_P_MAX },
  558. .p1 = { .min = IRONLAKE_LVDS_D_SSC_P1_MIN,.max = IRONLAKE_LVDS_D_SSC_P1_MAX },
  559. .p2 = { .dot_limit = IRONLAKE_P2_DOT_LIMIT,
  560. .p2_slow = IRONLAKE_LVDS_D_SSC_P2_SLOW,
  561. .p2_fast = IRONLAKE_LVDS_D_SSC_P2_FAST },
  562. .find_pll = intel_g4x_find_best_PLL,
  563. };
  564. static const intel_limit_t intel_limits_ironlake_display_port = {
  565. .dot = { .min = IRONLAKE_DOT_MIN,
  566. .max = IRONLAKE_DOT_MAX },
  567. .vco = { .min = IRONLAKE_VCO_MIN,
  568. .max = IRONLAKE_VCO_MAX},
  569. .n = { .min = IRONLAKE_DP_N_MIN,
  570. .max = IRONLAKE_DP_N_MAX },
  571. .m = { .min = IRONLAKE_DP_M_MIN,
  572. .max = IRONLAKE_DP_M_MAX },
  573. .m1 = { .min = IRONLAKE_M1_MIN,
  574. .max = IRONLAKE_M1_MAX },
  575. .m2 = { .min = IRONLAKE_M2_MIN,
  576. .max = IRONLAKE_M2_MAX },
  577. .p = { .min = IRONLAKE_DP_P_MIN,
  578. .max = IRONLAKE_DP_P_MAX },
  579. .p1 = { .min = IRONLAKE_DP_P1_MIN,
  580. .max = IRONLAKE_DP_P1_MAX},
  581. .p2 = { .dot_limit = IRONLAKE_DP_P2_LIMIT,
  582. .p2_slow = IRONLAKE_DP_P2_SLOW,
  583. .p2_fast = IRONLAKE_DP_P2_FAST },
  584. .find_pll = intel_find_pll_ironlake_dp,
  585. };
  586. static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc)
  587. {
  588. struct drm_device *dev = crtc->dev;
  589. struct drm_i915_private *dev_priv = dev->dev_private;
  590. const intel_limit_t *limit;
  591. int refclk = 120;
  592. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  593. if (dev_priv->lvds_use_ssc && dev_priv->lvds_ssc_freq == 100)
  594. refclk = 100;
  595. if ((I915_READ(PCH_LVDS) & LVDS_CLKB_POWER_MASK) ==
  596. LVDS_CLKB_POWER_UP) {
  597. /* LVDS dual channel */
  598. if (refclk == 100)
  599. limit = &intel_limits_ironlake_dual_lvds_100m;
  600. else
  601. limit = &intel_limits_ironlake_dual_lvds;
  602. } else {
  603. if (refclk == 100)
  604. limit = &intel_limits_ironlake_single_lvds_100m;
  605. else
  606. limit = &intel_limits_ironlake_single_lvds;
  607. }
  608. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  609. HAS_eDP)
  610. limit = &intel_limits_ironlake_display_port;
  611. else
  612. limit = &intel_limits_ironlake_dac;
  613. return limit;
  614. }
  615. static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
  616. {
  617. struct drm_device *dev = crtc->dev;
  618. struct drm_i915_private *dev_priv = dev->dev_private;
  619. const intel_limit_t *limit;
  620. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  621. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  622. LVDS_CLKB_POWER_UP)
  623. /* LVDS with dual channel */
  624. limit = &intel_limits_g4x_dual_channel_lvds;
  625. else
  626. /* LVDS with dual channel */
  627. limit = &intel_limits_g4x_single_channel_lvds;
  628. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
  629. intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
  630. limit = &intel_limits_g4x_hdmi;
  631. } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
  632. limit = &intel_limits_g4x_sdvo;
  633. } else if (intel_pipe_has_type (crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  634. limit = &intel_limits_g4x_display_port;
  635. } else /* The option is for other outputs */
  636. limit = &intel_limits_i9xx_sdvo;
  637. return limit;
  638. }
  639. static const intel_limit_t *intel_limit(struct drm_crtc *crtc)
  640. {
  641. struct drm_device *dev = crtc->dev;
  642. const intel_limit_t *limit;
  643. if (HAS_PCH_SPLIT(dev))
  644. limit = intel_ironlake_limit(crtc);
  645. else if (IS_G4X(dev)) {
  646. limit = intel_g4x_limit(crtc);
  647. } else if (IS_I9XX(dev) && !IS_PINEVIEW(dev)) {
  648. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  649. limit = &intel_limits_i9xx_lvds;
  650. else
  651. limit = &intel_limits_i9xx_sdvo;
  652. } else if (IS_PINEVIEW(dev)) {
  653. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  654. limit = &intel_limits_pineview_lvds;
  655. else
  656. limit = &intel_limits_pineview_sdvo;
  657. } else {
  658. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
  659. limit = &intel_limits_i8xx_lvds;
  660. else
  661. limit = &intel_limits_i8xx_dvo;
  662. }
  663. return limit;
  664. }
  665. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  666. static void pineview_clock(int refclk, intel_clock_t *clock)
  667. {
  668. clock->m = clock->m2 + 2;
  669. clock->p = clock->p1 * clock->p2;
  670. clock->vco = refclk * clock->m / clock->n;
  671. clock->dot = clock->vco / clock->p;
  672. }
  673. static void intel_clock(struct drm_device *dev, int refclk, intel_clock_t *clock)
  674. {
  675. if (IS_PINEVIEW(dev)) {
  676. pineview_clock(refclk, clock);
  677. return;
  678. }
  679. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  680. clock->p = clock->p1 * clock->p2;
  681. clock->vco = refclk * clock->m / (clock->n + 2);
  682. clock->dot = clock->vco / clock->p;
  683. }
  684. /**
  685. * Returns whether any output on the specified pipe is of the specified type
  686. */
  687. bool intel_pipe_has_type (struct drm_crtc *crtc, int type)
  688. {
  689. struct drm_device *dev = crtc->dev;
  690. struct drm_mode_config *mode_config = &dev->mode_config;
  691. struct drm_encoder *l_entry;
  692. list_for_each_entry(l_entry, &mode_config->encoder_list, head) {
  693. if (l_entry && l_entry->crtc == crtc) {
  694. struct intel_encoder *intel_encoder = enc_to_intel_encoder(l_entry);
  695. if (intel_encoder->type == type)
  696. return true;
  697. }
  698. }
  699. return false;
  700. }
  701. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  702. /**
  703. * Returns whether the given set of divisors are valid for a given refclk with
  704. * the given connectors.
  705. */
  706. static bool intel_PLL_is_valid(struct drm_crtc *crtc, intel_clock_t *clock)
  707. {
  708. const intel_limit_t *limit = intel_limit (crtc);
  709. struct drm_device *dev = crtc->dev;
  710. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  711. INTELPllInvalid ("p1 out of range\n");
  712. if (clock->p < limit->p.min || limit->p.max < clock->p)
  713. INTELPllInvalid ("p out of range\n");
  714. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  715. INTELPllInvalid ("m2 out of range\n");
  716. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  717. INTELPllInvalid ("m1 out of range\n");
  718. if (clock->m1 <= clock->m2 && !IS_PINEVIEW(dev))
  719. INTELPllInvalid ("m1 <= m2\n");
  720. if (clock->m < limit->m.min || limit->m.max < clock->m)
  721. INTELPllInvalid ("m out of range\n");
  722. if (clock->n < limit->n.min || limit->n.max < clock->n)
  723. INTELPllInvalid ("n out of range\n");
  724. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  725. INTELPllInvalid ("vco out of range\n");
  726. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  727. * connector, etc., rather than just a single range.
  728. */
  729. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  730. INTELPllInvalid ("dot out of range\n");
  731. return true;
  732. }
  733. static bool
  734. intel_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  735. int target, int refclk, intel_clock_t *best_clock)
  736. {
  737. struct drm_device *dev = crtc->dev;
  738. struct drm_i915_private *dev_priv = dev->dev_private;
  739. intel_clock_t clock;
  740. int err = target;
  741. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  742. (I915_READ(LVDS)) != 0) {
  743. /*
  744. * For LVDS, if the panel is on, just rely on its current
  745. * settings for dual-channel. We haven't figured out how to
  746. * reliably set up different single/dual channel state, if we
  747. * even can.
  748. */
  749. if ((I915_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  750. LVDS_CLKB_POWER_UP)
  751. clock.p2 = limit->p2.p2_fast;
  752. else
  753. clock.p2 = limit->p2.p2_slow;
  754. } else {
  755. if (target < limit->p2.dot_limit)
  756. clock.p2 = limit->p2.p2_slow;
  757. else
  758. clock.p2 = limit->p2.p2_fast;
  759. }
  760. memset (best_clock, 0, sizeof (*best_clock));
  761. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  762. clock.m1++) {
  763. for (clock.m2 = limit->m2.min;
  764. clock.m2 <= limit->m2.max; clock.m2++) {
  765. /* m1 is always 0 in Pineview */
  766. if (clock.m2 >= clock.m1 && !IS_PINEVIEW(dev))
  767. break;
  768. for (clock.n = limit->n.min;
  769. clock.n <= limit->n.max; clock.n++) {
  770. for (clock.p1 = limit->p1.min;
  771. clock.p1 <= limit->p1.max; clock.p1++) {
  772. int this_err;
  773. intel_clock(dev, refclk, &clock);
  774. if (!intel_PLL_is_valid(crtc, &clock))
  775. continue;
  776. this_err = abs(clock.dot - target);
  777. if (this_err < err) {
  778. *best_clock = clock;
  779. err = this_err;
  780. }
  781. }
  782. }
  783. }
  784. }
  785. return (err != target);
  786. }
  787. static bool
  788. intel_g4x_find_best_PLL(const intel_limit_t *limit, struct drm_crtc *crtc,
  789. int target, int refclk, intel_clock_t *best_clock)
  790. {
  791. struct drm_device *dev = crtc->dev;
  792. struct drm_i915_private *dev_priv = dev->dev_private;
  793. intel_clock_t clock;
  794. int max_n;
  795. bool found;
  796. /* approximately equals target * 0.00585 */
  797. int err_most = (target >> 8) + (target >> 9);
  798. found = false;
  799. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  800. int lvds_reg;
  801. if (HAS_PCH_SPLIT(dev))
  802. lvds_reg = PCH_LVDS;
  803. else
  804. lvds_reg = LVDS;
  805. if ((I915_READ(lvds_reg) & LVDS_CLKB_POWER_MASK) ==
  806. LVDS_CLKB_POWER_UP)
  807. clock.p2 = limit->p2.p2_fast;
  808. else
  809. clock.p2 = limit->p2.p2_slow;
  810. } else {
  811. if (target < limit->p2.dot_limit)
  812. clock.p2 = limit->p2.p2_slow;
  813. else
  814. clock.p2 = limit->p2.p2_fast;
  815. }
  816. memset(best_clock, 0, sizeof(*best_clock));
  817. max_n = limit->n.max;
  818. /* based on hardware requirement, prefer smaller n to precision */
  819. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  820. /* based on hardware requirement, prefere larger m1,m2 */
  821. for (clock.m1 = limit->m1.max;
  822. clock.m1 >= limit->m1.min; clock.m1--) {
  823. for (clock.m2 = limit->m2.max;
  824. clock.m2 >= limit->m2.min; clock.m2--) {
  825. for (clock.p1 = limit->p1.max;
  826. clock.p1 >= limit->p1.min; clock.p1--) {
  827. int this_err;
  828. intel_clock(dev, refclk, &clock);
  829. if (!intel_PLL_is_valid(crtc, &clock))
  830. continue;
  831. this_err = abs(clock.dot - target) ;
  832. if (this_err < err_most) {
  833. *best_clock = clock;
  834. err_most = this_err;
  835. max_n = clock.n;
  836. found = true;
  837. }
  838. }
  839. }
  840. }
  841. }
  842. return found;
  843. }
  844. static bool
  845. intel_find_pll_ironlake_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  846. int target, int refclk, intel_clock_t *best_clock)
  847. {
  848. struct drm_device *dev = crtc->dev;
  849. intel_clock_t clock;
  850. /* return directly when it is eDP */
  851. if (HAS_eDP)
  852. return true;
  853. if (target < 200000) {
  854. clock.n = 1;
  855. clock.p1 = 2;
  856. clock.p2 = 10;
  857. clock.m1 = 12;
  858. clock.m2 = 9;
  859. } else {
  860. clock.n = 2;
  861. clock.p1 = 1;
  862. clock.p2 = 10;
  863. clock.m1 = 14;
  864. clock.m2 = 8;
  865. }
  866. intel_clock(dev, refclk, &clock);
  867. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  868. return true;
  869. }
  870. /* DisplayPort has only two frequencies, 162MHz and 270MHz */
  871. static bool
  872. intel_find_pll_g4x_dp(const intel_limit_t *limit, struct drm_crtc *crtc,
  873. int target, int refclk, intel_clock_t *best_clock)
  874. {
  875. intel_clock_t clock;
  876. if (target < 200000) {
  877. clock.p1 = 2;
  878. clock.p2 = 10;
  879. clock.n = 2;
  880. clock.m1 = 23;
  881. clock.m2 = 8;
  882. } else {
  883. clock.p1 = 1;
  884. clock.p2 = 10;
  885. clock.n = 1;
  886. clock.m1 = 14;
  887. clock.m2 = 2;
  888. }
  889. clock.m = 5 * (clock.m1 + 2) + (clock.m2 + 2);
  890. clock.p = (clock.p1 * clock.p2);
  891. clock.dot = 96000 * clock.m / (clock.n + 2) / clock.p;
  892. clock.vco = 0;
  893. memcpy(best_clock, &clock, sizeof(intel_clock_t));
  894. return true;
  895. }
  896. void
  897. intel_wait_for_vblank(struct drm_device *dev)
  898. {
  899. /* Wait for 20ms, i.e. one cycle at 50hz. */
  900. if (in_dbg_master())
  901. mdelay(20); /* The kernel debugger cannot call msleep() */
  902. else
  903. msleep(20);
  904. }
  905. /* Parameters have changed, update FBC info */
  906. static void i8xx_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  907. {
  908. struct drm_device *dev = crtc->dev;
  909. struct drm_i915_private *dev_priv = dev->dev_private;
  910. struct drm_framebuffer *fb = crtc->fb;
  911. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  912. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  914. int plane, i;
  915. u32 fbc_ctl, fbc_ctl2;
  916. dev_priv->cfb_pitch = dev_priv->cfb_size / FBC_LL_SIZE;
  917. if (fb->pitch < dev_priv->cfb_pitch)
  918. dev_priv->cfb_pitch = fb->pitch;
  919. /* FBC_CTL wants 64B units */
  920. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  921. dev_priv->cfb_fence = obj_priv->fence_reg;
  922. dev_priv->cfb_plane = intel_crtc->plane;
  923. plane = dev_priv->cfb_plane == 0 ? FBC_CTL_PLANEA : FBC_CTL_PLANEB;
  924. /* Clear old tags */
  925. for (i = 0; i < (FBC_LL_SIZE / 32) + 1; i++)
  926. I915_WRITE(FBC_TAG + (i * 4), 0);
  927. /* Set it up... */
  928. fbc_ctl2 = FBC_CTL_FENCE_DBL | FBC_CTL_IDLE_IMM | plane;
  929. if (obj_priv->tiling_mode != I915_TILING_NONE)
  930. fbc_ctl2 |= FBC_CTL_CPU_FENCE;
  931. I915_WRITE(FBC_CONTROL2, fbc_ctl2);
  932. I915_WRITE(FBC_FENCE_OFF, crtc->y);
  933. /* enable it... */
  934. fbc_ctl = FBC_CTL_EN | FBC_CTL_PERIODIC;
  935. if (IS_I945GM(dev))
  936. fbc_ctl |= FBC_CTL_C3_IDLE; /* 945 needs special SR handling */
  937. fbc_ctl |= (dev_priv->cfb_pitch & 0xff) << FBC_CTL_STRIDE_SHIFT;
  938. fbc_ctl |= (interval & 0x2fff) << FBC_CTL_INTERVAL_SHIFT;
  939. if (obj_priv->tiling_mode != I915_TILING_NONE)
  940. fbc_ctl |= dev_priv->cfb_fence;
  941. I915_WRITE(FBC_CONTROL, fbc_ctl);
  942. DRM_DEBUG_KMS("enabled FBC, pitch %ld, yoff %d, plane %d, ",
  943. dev_priv->cfb_pitch, crtc->y, dev_priv->cfb_plane);
  944. }
  945. void i8xx_disable_fbc(struct drm_device *dev)
  946. {
  947. struct drm_i915_private *dev_priv = dev->dev_private;
  948. unsigned long timeout = jiffies + msecs_to_jiffies(1);
  949. u32 fbc_ctl;
  950. if (!I915_HAS_FBC(dev))
  951. return;
  952. if (!(I915_READ(FBC_CONTROL) & FBC_CTL_EN))
  953. return; /* Already off, just return */
  954. /* Disable compression */
  955. fbc_ctl = I915_READ(FBC_CONTROL);
  956. fbc_ctl &= ~FBC_CTL_EN;
  957. I915_WRITE(FBC_CONTROL, fbc_ctl);
  958. /* Wait for compressing bit to clear */
  959. while (I915_READ(FBC_STATUS) & FBC_STAT_COMPRESSING) {
  960. if (time_after(jiffies, timeout)) {
  961. DRM_DEBUG_DRIVER("FBC idle timed out\n");
  962. break;
  963. }
  964. ; /* do nothing */
  965. }
  966. intel_wait_for_vblank(dev);
  967. DRM_DEBUG_KMS("disabled FBC\n");
  968. }
  969. static bool i8xx_fbc_enabled(struct drm_device *dev)
  970. {
  971. struct drm_i915_private *dev_priv = dev->dev_private;
  972. return I915_READ(FBC_CONTROL) & FBC_CTL_EN;
  973. }
  974. static void g4x_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  975. {
  976. struct drm_device *dev = crtc->dev;
  977. struct drm_i915_private *dev_priv = dev->dev_private;
  978. struct drm_framebuffer *fb = crtc->fb;
  979. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  980. struct drm_i915_gem_object *obj_priv = to_intel_bo(intel_fb->obj);
  981. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  982. int plane = (intel_crtc->plane == 0 ? DPFC_CTL_PLANEA :
  983. DPFC_CTL_PLANEB);
  984. unsigned long stall_watermark = 200;
  985. u32 dpfc_ctl;
  986. dev_priv->cfb_pitch = (dev_priv->cfb_pitch / 64) - 1;
  987. dev_priv->cfb_fence = obj_priv->fence_reg;
  988. dev_priv->cfb_plane = intel_crtc->plane;
  989. dpfc_ctl = plane | DPFC_SR_EN | DPFC_CTL_LIMIT_1X;
  990. if (obj_priv->tiling_mode != I915_TILING_NONE) {
  991. dpfc_ctl |= DPFC_CTL_FENCE_EN | dev_priv->cfb_fence;
  992. I915_WRITE(DPFC_CHICKEN, DPFC_HT_MODIFY);
  993. } else {
  994. I915_WRITE(DPFC_CHICKEN, ~DPFC_HT_MODIFY);
  995. }
  996. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  997. I915_WRITE(DPFC_RECOMP_CTL, DPFC_RECOMP_STALL_EN |
  998. (stall_watermark << DPFC_RECOMP_STALL_WM_SHIFT) |
  999. (interval << DPFC_RECOMP_TIMER_COUNT_SHIFT));
  1000. I915_WRITE(DPFC_FENCE_YOFF, crtc->y);
  1001. /* enable it... */
  1002. I915_WRITE(DPFC_CONTROL, I915_READ(DPFC_CONTROL) | DPFC_CTL_EN);
  1003. DRM_DEBUG_KMS("enabled fbc on plane %d\n", intel_crtc->plane);
  1004. }
  1005. void g4x_disable_fbc(struct drm_device *dev)
  1006. {
  1007. struct drm_i915_private *dev_priv = dev->dev_private;
  1008. u32 dpfc_ctl;
  1009. /* Disable compression */
  1010. dpfc_ctl = I915_READ(DPFC_CONTROL);
  1011. dpfc_ctl &= ~DPFC_CTL_EN;
  1012. I915_WRITE(DPFC_CONTROL, dpfc_ctl);
  1013. intel_wait_for_vblank(dev);
  1014. DRM_DEBUG_KMS("disabled FBC\n");
  1015. }
  1016. static bool g4x_fbc_enabled(struct drm_device *dev)
  1017. {
  1018. struct drm_i915_private *dev_priv = dev->dev_private;
  1019. return I915_READ(DPFC_CONTROL) & DPFC_CTL_EN;
  1020. }
  1021. bool intel_fbc_enabled(struct drm_device *dev)
  1022. {
  1023. struct drm_i915_private *dev_priv = dev->dev_private;
  1024. if (!dev_priv->display.fbc_enabled)
  1025. return false;
  1026. return dev_priv->display.fbc_enabled(dev);
  1027. }
  1028. void intel_enable_fbc(struct drm_crtc *crtc, unsigned long interval)
  1029. {
  1030. struct drm_i915_private *dev_priv = crtc->dev->dev_private;
  1031. if (!dev_priv->display.enable_fbc)
  1032. return;
  1033. dev_priv->display.enable_fbc(crtc, interval);
  1034. }
  1035. void intel_disable_fbc(struct drm_device *dev)
  1036. {
  1037. struct drm_i915_private *dev_priv = dev->dev_private;
  1038. if (!dev_priv->display.disable_fbc)
  1039. return;
  1040. dev_priv->display.disable_fbc(dev);
  1041. }
  1042. /**
  1043. * intel_update_fbc - enable/disable FBC as needed
  1044. * @crtc: CRTC to point the compressor at
  1045. * @mode: mode in use
  1046. *
  1047. * Set up the framebuffer compression hardware at mode set time. We
  1048. * enable it if possible:
  1049. * - plane A only (on pre-965)
  1050. * - no pixel mulitply/line duplication
  1051. * - no alpha buffer discard
  1052. * - no dual wide
  1053. * - framebuffer <= 2048 in width, 1536 in height
  1054. *
  1055. * We can't assume that any compression will take place (worst case),
  1056. * so the compressed buffer has to be the same size as the uncompressed
  1057. * one. It also must reside (along with the line length buffer) in
  1058. * stolen memory.
  1059. *
  1060. * We need to enable/disable FBC on a global basis.
  1061. */
  1062. static void intel_update_fbc(struct drm_crtc *crtc,
  1063. struct drm_display_mode *mode)
  1064. {
  1065. struct drm_device *dev = crtc->dev;
  1066. struct drm_i915_private *dev_priv = dev->dev_private;
  1067. struct drm_framebuffer *fb = crtc->fb;
  1068. struct intel_framebuffer *intel_fb;
  1069. struct drm_i915_gem_object *obj_priv;
  1070. struct drm_crtc *tmp_crtc;
  1071. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1072. int plane = intel_crtc->plane;
  1073. int crtcs_enabled = 0;
  1074. DRM_DEBUG_KMS("\n");
  1075. if (!i915_powersave)
  1076. return;
  1077. if (!I915_HAS_FBC(dev))
  1078. return;
  1079. if (!crtc->fb)
  1080. return;
  1081. intel_fb = to_intel_framebuffer(fb);
  1082. obj_priv = to_intel_bo(intel_fb->obj);
  1083. /*
  1084. * If FBC is already on, we just have to verify that we can
  1085. * keep it that way...
  1086. * Need to disable if:
  1087. * - more than one pipe is active
  1088. * - changing FBC params (stride, fence, mode)
  1089. * - new fb is too large to fit in compressed buffer
  1090. * - going to an unsupported config (interlace, pixel multiply, etc.)
  1091. */
  1092. list_for_each_entry(tmp_crtc, &dev->mode_config.crtc_list, head) {
  1093. if (tmp_crtc->enabled)
  1094. crtcs_enabled++;
  1095. }
  1096. DRM_DEBUG_KMS("%d pipes active\n", crtcs_enabled);
  1097. if (crtcs_enabled > 1) {
  1098. DRM_DEBUG_KMS("more than one pipe active, disabling compression\n");
  1099. dev_priv->no_fbc_reason = FBC_MULTIPLE_PIPES;
  1100. goto out_disable;
  1101. }
  1102. if (intel_fb->obj->size > dev_priv->cfb_size) {
  1103. DRM_DEBUG_KMS("framebuffer too large, disabling "
  1104. "compression\n");
  1105. dev_priv->no_fbc_reason = FBC_STOLEN_TOO_SMALL;
  1106. goto out_disable;
  1107. }
  1108. if ((mode->flags & DRM_MODE_FLAG_INTERLACE) ||
  1109. (mode->flags & DRM_MODE_FLAG_DBLSCAN)) {
  1110. DRM_DEBUG_KMS("mode incompatible with compression, "
  1111. "disabling\n");
  1112. dev_priv->no_fbc_reason = FBC_UNSUPPORTED_MODE;
  1113. goto out_disable;
  1114. }
  1115. if ((mode->hdisplay > 2048) ||
  1116. (mode->vdisplay > 1536)) {
  1117. DRM_DEBUG_KMS("mode too large for compression, disabling\n");
  1118. dev_priv->no_fbc_reason = FBC_MODE_TOO_LARGE;
  1119. goto out_disable;
  1120. }
  1121. if ((IS_I915GM(dev) || IS_I945GM(dev)) && plane != 0) {
  1122. DRM_DEBUG_KMS("plane not 0, disabling compression\n");
  1123. dev_priv->no_fbc_reason = FBC_BAD_PLANE;
  1124. goto out_disable;
  1125. }
  1126. if (obj_priv->tiling_mode != I915_TILING_X) {
  1127. DRM_DEBUG_KMS("framebuffer not tiled, disabling compression\n");
  1128. dev_priv->no_fbc_reason = FBC_NOT_TILED;
  1129. goto out_disable;
  1130. }
  1131. /* If the kernel debugger is active, always disable compression */
  1132. if (in_dbg_master())
  1133. goto out_disable;
  1134. if (intel_fbc_enabled(dev)) {
  1135. /* We can re-enable it in this case, but need to update pitch */
  1136. if ((fb->pitch > dev_priv->cfb_pitch) ||
  1137. (obj_priv->fence_reg != dev_priv->cfb_fence) ||
  1138. (plane != dev_priv->cfb_plane))
  1139. intel_disable_fbc(dev);
  1140. }
  1141. /* Now try to turn it back on if possible */
  1142. if (!intel_fbc_enabled(dev))
  1143. intel_enable_fbc(crtc, 500);
  1144. return;
  1145. out_disable:
  1146. /* Multiple disables should be harmless */
  1147. if (intel_fbc_enabled(dev)) {
  1148. DRM_DEBUG_KMS("unsupported config, disabling FBC\n");
  1149. intel_disable_fbc(dev);
  1150. }
  1151. }
  1152. int
  1153. intel_pin_and_fence_fb_obj(struct drm_device *dev, struct drm_gem_object *obj)
  1154. {
  1155. struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
  1156. u32 alignment;
  1157. int ret;
  1158. switch (obj_priv->tiling_mode) {
  1159. case I915_TILING_NONE:
  1160. alignment = 64 * 1024;
  1161. break;
  1162. case I915_TILING_X:
  1163. /* pin() will align the object as required by fence */
  1164. alignment = 0;
  1165. break;
  1166. case I915_TILING_Y:
  1167. /* FIXME: Is this true? */
  1168. DRM_ERROR("Y tiled not allowed for scan out buffers\n");
  1169. return -EINVAL;
  1170. default:
  1171. BUG();
  1172. }
  1173. ret = i915_gem_object_pin(obj, alignment);
  1174. if (ret != 0)
  1175. return ret;
  1176. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1177. * fence, whereas 965+ only requires a fence if using
  1178. * framebuffer compression. For simplicity, we always install
  1179. * a fence as the cost is not that onerous.
  1180. */
  1181. if (obj_priv->fence_reg == I915_FENCE_REG_NONE &&
  1182. obj_priv->tiling_mode != I915_TILING_NONE) {
  1183. ret = i915_gem_object_get_fence_reg(obj);
  1184. if (ret != 0) {
  1185. i915_gem_object_unpin(obj);
  1186. return ret;
  1187. }
  1188. }
  1189. return 0;
  1190. }
  1191. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  1192. static int
  1193. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  1194. int x, int y)
  1195. {
  1196. struct drm_device *dev = crtc->dev;
  1197. struct drm_i915_private *dev_priv = dev->dev_private;
  1198. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1199. struct intel_framebuffer *intel_fb;
  1200. struct drm_i915_gem_object *obj_priv;
  1201. struct drm_gem_object *obj;
  1202. int plane = intel_crtc->plane;
  1203. unsigned long Start, Offset;
  1204. int dspbase = (plane == 0 ? DSPAADDR : DSPBADDR);
  1205. int dspsurf = (plane == 0 ? DSPASURF : DSPBSURF);
  1206. int dspstride = (plane == 0) ? DSPASTRIDE : DSPBSTRIDE;
  1207. int dsptileoff = (plane == 0 ? DSPATILEOFF : DSPBTILEOFF);
  1208. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1209. u32 dspcntr;
  1210. switch (plane) {
  1211. case 0:
  1212. case 1:
  1213. break;
  1214. default:
  1215. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1216. return -EINVAL;
  1217. }
  1218. intel_fb = to_intel_framebuffer(fb);
  1219. obj = intel_fb->obj;
  1220. obj_priv = to_intel_bo(obj);
  1221. dspcntr = I915_READ(dspcntr_reg);
  1222. /* Mask out pixel format bits in case we change it */
  1223. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1224. switch (fb->bits_per_pixel) {
  1225. case 8:
  1226. dspcntr |= DISPPLANE_8BPP;
  1227. break;
  1228. case 16:
  1229. if (fb->depth == 15)
  1230. dspcntr |= DISPPLANE_15_16BPP;
  1231. else
  1232. dspcntr |= DISPPLANE_16BPP;
  1233. break;
  1234. case 24:
  1235. case 32:
  1236. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1237. break;
  1238. default:
  1239. DRM_ERROR("Unknown color depth\n");
  1240. return -EINVAL;
  1241. }
  1242. if (IS_I965G(dev)) {
  1243. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1244. dspcntr |= DISPPLANE_TILED;
  1245. else
  1246. dspcntr &= ~DISPPLANE_TILED;
  1247. }
  1248. if (IS_IRONLAKE(dev))
  1249. /* must disable */
  1250. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1251. I915_WRITE(dspcntr_reg, dspcntr);
  1252. Start = obj_priv->gtt_offset;
  1253. Offset = y * fb->pitch + x * (fb->bits_per_pixel / 8);
  1254. DRM_DEBUG("Writing base %08lX %08lX %d %d\n", Start, Offset, x, y);
  1255. I915_WRITE(dspstride, fb->pitch);
  1256. if (IS_I965G(dev)) {
  1257. I915_WRITE(dspbase, Offset);
  1258. I915_READ(dspbase);
  1259. I915_WRITE(dspsurf, Start);
  1260. I915_READ(dspsurf);
  1261. I915_WRITE(dsptileoff, (y << 16) | x);
  1262. } else {
  1263. I915_WRITE(dspbase, Start + Offset);
  1264. I915_READ(dspbase);
  1265. }
  1266. if ((IS_I965G(dev) || plane == 0))
  1267. intel_update_fbc(crtc, &crtc->mode);
  1268. intel_wait_for_vblank(dev);
  1269. intel_increase_pllclock(crtc, true);
  1270. return 0;
  1271. }
  1272. static int
  1273. intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
  1274. struct drm_framebuffer *old_fb)
  1275. {
  1276. struct drm_device *dev = crtc->dev;
  1277. struct drm_i915_private *dev_priv = dev->dev_private;
  1278. struct drm_i915_master_private *master_priv;
  1279. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1280. struct intel_framebuffer *intel_fb;
  1281. struct drm_i915_gem_object *obj_priv;
  1282. struct drm_gem_object *obj;
  1283. int pipe = intel_crtc->pipe;
  1284. int plane = intel_crtc->plane;
  1285. unsigned long Start, Offset;
  1286. int dspbase = (plane == 0 ? DSPAADDR : DSPBADDR);
  1287. int dspsurf = (plane == 0 ? DSPASURF : DSPBSURF);
  1288. int dspstride = (plane == 0) ? DSPASTRIDE : DSPBSTRIDE;
  1289. int dsptileoff = (plane == 0 ? DSPATILEOFF : DSPBTILEOFF);
  1290. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1291. u32 dspcntr;
  1292. int ret;
  1293. /* no fb bound */
  1294. if (!crtc->fb) {
  1295. DRM_DEBUG_KMS("No FB bound\n");
  1296. return 0;
  1297. }
  1298. switch (plane) {
  1299. case 0:
  1300. case 1:
  1301. break;
  1302. default:
  1303. DRM_ERROR("Can't update plane %d in SAREA\n", plane);
  1304. return -EINVAL;
  1305. }
  1306. intel_fb = to_intel_framebuffer(crtc->fb);
  1307. obj = intel_fb->obj;
  1308. obj_priv = to_intel_bo(obj);
  1309. mutex_lock(&dev->struct_mutex);
  1310. ret = intel_pin_and_fence_fb_obj(dev, obj);
  1311. if (ret != 0) {
  1312. mutex_unlock(&dev->struct_mutex);
  1313. return ret;
  1314. }
  1315. ret = i915_gem_object_set_to_display_plane(obj);
  1316. if (ret != 0) {
  1317. i915_gem_object_unpin(obj);
  1318. mutex_unlock(&dev->struct_mutex);
  1319. return ret;
  1320. }
  1321. dspcntr = I915_READ(dspcntr_reg);
  1322. /* Mask out pixel format bits in case we change it */
  1323. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  1324. switch (crtc->fb->bits_per_pixel) {
  1325. case 8:
  1326. dspcntr |= DISPPLANE_8BPP;
  1327. break;
  1328. case 16:
  1329. if (crtc->fb->depth == 15)
  1330. dspcntr |= DISPPLANE_15_16BPP;
  1331. else
  1332. dspcntr |= DISPPLANE_16BPP;
  1333. break;
  1334. case 24:
  1335. case 32:
  1336. if (crtc->fb->depth == 30)
  1337. dspcntr |= DISPPLANE_32BPP_30BIT_NO_ALPHA;
  1338. else
  1339. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  1340. break;
  1341. default:
  1342. DRM_ERROR("Unknown color depth\n");
  1343. i915_gem_object_unpin(obj);
  1344. mutex_unlock(&dev->struct_mutex);
  1345. return -EINVAL;
  1346. }
  1347. if (IS_I965G(dev)) {
  1348. if (obj_priv->tiling_mode != I915_TILING_NONE)
  1349. dspcntr |= DISPPLANE_TILED;
  1350. else
  1351. dspcntr &= ~DISPPLANE_TILED;
  1352. }
  1353. if (HAS_PCH_SPLIT(dev))
  1354. /* must disable */
  1355. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  1356. I915_WRITE(dspcntr_reg, dspcntr);
  1357. Start = obj_priv->gtt_offset;
  1358. Offset = y * crtc->fb->pitch + x * (crtc->fb->bits_per_pixel / 8);
  1359. DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
  1360. Start, Offset, x, y, crtc->fb->pitch);
  1361. I915_WRITE(dspstride, crtc->fb->pitch);
  1362. if (IS_I965G(dev)) {
  1363. I915_WRITE(dspbase, Offset);
  1364. I915_READ(dspbase);
  1365. I915_WRITE(dspsurf, Start);
  1366. I915_READ(dspsurf);
  1367. I915_WRITE(dsptileoff, (y << 16) | x);
  1368. } else {
  1369. I915_WRITE(dspbase, Start + Offset);
  1370. I915_READ(dspbase);
  1371. }
  1372. if ((IS_I965G(dev) || plane == 0))
  1373. intel_update_fbc(crtc, &crtc->mode);
  1374. intel_wait_for_vblank(dev);
  1375. if (old_fb) {
  1376. intel_fb = to_intel_framebuffer(old_fb);
  1377. obj_priv = to_intel_bo(intel_fb->obj);
  1378. i915_gem_object_unpin(intel_fb->obj);
  1379. }
  1380. intel_increase_pllclock(crtc, true);
  1381. mutex_unlock(&dev->struct_mutex);
  1382. if (!dev->primary->master)
  1383. return 0;
  1384. master_priv = dev->primary->master->driver_priv;
  1385. if (!master_priv->sarea_priv)
  1386. return 0;
  1387. if (pipe) {
  1388. master_priv->sarea_priv->pipeB_x = x;
  1389. master_priv->sarea_priv->pipeB_y = y;
  1390. } else {
  1391. master_priv->sarea_priv->pipeA_x = x;
  1392. master_priv->sarea_priv->pipeA_y = y;
  1393. }
  1394. return 0;
  1395. }
  1396. /* Disable the VGA plane that we never use */
  1397. static void i915_disable_vga (struct drm_device *dev)
  1398. {
  1399. struct drm_i915_private *dev_priv = dev->dev_private;
  1400. u8 sr1;
  1401. u32 vga_reg;
  1402. if (HAS_PCH_SPLIT(dev))
  1403. vga_reg = CPU_VGACNTRL;
  1404. else
  1405. vga_reg = VGACNTRL;
  1406. if (I915_READ(vga_reg) & VGA_DISP_DISABLE)
  1407. return;
  1408. I915_WRITE8(VGA_SR_INDEX, 1);
  1409. sr1 = I915_READ8(VGA_SR_DATA);
  1410. I915_WRITE8(VGA_SR_DATA, sr1 | (1 << 5));
  1411. udelay(100);
  1412. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  1413. }
  1414. static void ironlake_disable_pll_edp (struct drm_crtc *crtc)
  1415. {
  1416. struct drm_device *dev = crtc->dev;
  1417. struct drm_i915_private *dev_priv = dev->dev_private;
  1418. u32 dpa_ctl;
  1419. DRM_DEBUG_KMS("\n");
  1420. dpa_ctl = I915_READ(DP_A);
  1421. dpa_ctl &= ~DP_PLL_ENABLE;
  1422. I915_WRITE(DP_A, dpa_ctl);
  1423. }
  1424. static void ironlake_enable_pll_edp (struct drm_crtc *crtc)
  1425. {
  1426. struct drm_device *dev = crtc->dev;
  1427. struct drm_i915_private *dev_priv = dev->dev_private;
  1428. u32 dpa_ctl;
  1429. dpa_ctl = I915_READ(DP_A);
  1430. dpa_ctl |= DP_PLL_ENABLE;
  1431. I915_WRITE(DP_A, dpa_ctl);
  1432. udelay(200);
  1433. }
  1434. static void ironlake_set_pll_edp (struct drm_crtc *crtc, int clock)
  1435. {
  1436. struct drm_device *dev = crtc->dev;
  1437. struct drm_i915_private *dev_priv = dev->dev_private;
  1438. u32 dpa_ctl;
  1439. DRM_DEBUG_KMS("eDP PLL enable for clock %d\n", clock);
  1440. dpa_ctl = I915_READ(DP_A);
  1441. dpa_ctl &= ~DP_PLL_FREQ_MASK;
  1442. if (clock < 200000) {
  1443. u32 temp;
  1444. dpa_ctl |= DP_PLL_FREQ_160MHZ;
  1445. /* workaround for 160Mhz:
  1446. 1) program 0x4600c bits 15:0 = 0x8124
  1447. 2) program 0x46010 bit 0 = 1
  1448. 3) program 0x46034 bit 24 = 1
  1449. 4) program 0x64000 bit 14 = 1
  1450. */
  1451. temp = I915_READ(0x4600c);
  1452. temp &= 0xffff0000;
  1453. I915_WRITE(0x4600c, temp | 0x8124);
  1454. temp = I915_READ(0x46010);
  1455. I915_WRITE(0x46010, temp | 1);
  1456. temp = I915_READ(0x46034);
  1457. I915_WRITE(0x46034, temp | (1 << 24));
  1458. } else {
  1459. dpa_ctl |= DP_PLL_FREQ_270MHZ;
  1460. }
  1461. I915_WRITE(DP_A, dpa_ctl);
  1462. udelay(500);
  1463. }
  1464. /* The FDI link training functions for ILK/Ibexpeak. */
  1465. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  1466. {
  1467. struct drm_device *dev = crtc->dev;
  1468. struct drm_i915_private *dev_priv = dev->dev_private;
  1469. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1470. int pipe = intel_crtc->pipe;
  1471. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1472. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1473. int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
  1474. int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
  1475. u32 temp, tries = 0;
  1476. /* enable CPU FDI TX and PCH FDI RX */
  1477. temp = I915_READ(fdi_tx_reg);
  1478. temp |= FDI_TX_ENABLE;
  1479. temp &= ~(7 << 19);
  1480. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1481. temp &= ~FDI_LINK_TRAIN_NONE;
  1482. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1483. I915_WRITE(fdi_tx_reg, temp);
  1484. I915_READ(fdi_tx_reg);
  1485. temp = I915_READ(fdi_rx_reg);
  1486. temp &= ~FDI_LINK_TRAIN_NONE;
  1487. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1488. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
  1489. I915_READ(fdi_rx_reg);
  1490. udelay(150);
  1491. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1492. for train result */
  1493. temp = I915_READ(fdi_rx_imr_reg);
  1494. temp &= ~FDI_RX_SYMBOL_LOCK;
  1495. temp &= ~FDI_RX_BIT_LOCK;
  1496. I915_WRITE(fdi_rx_imr_reg, temp);
  1497. I915_READ(fdi_rx_imr_reg);
  1498. udelay(150);
  1499. for (;;) {
  1500. temp = I915_READ(fdi_rx_iir_reg);
  1501. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1502. if ((temp & FDI_RX_BIT_LOCK)) {
  1503. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1504. I915_WRITE(fdi_rx_iir_reg,
  1505. temp | FDI_RX_BIT_LOCK);
  1506. break;
  1507. }
  1508. tries++;
  1509. if (tries > 5) {
  1510. DRM_DEBUG_KMS("FDI train 1 fail!\n");
  1511. break;
  1512. }
  1513. }
  1514. /* Train 2 */
  1515. temp = I915_READ(fdi_tx_reg);
  1516. temp &= ~FDI_LINK_TRAIN_NONE;
  1517. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1518. I915_WRITE(fdi_tx_reg, temp);
  1519. temp = I915_READ(fdi_rx_reg);
  1520. temp &= ~FDI_LINK_TRAIN_NONE;
  1521. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1522. I915_WRITE(fdi_rx_reg, temp);
  1523. udelay(150);
  1524. tries = 0;
  1525. for (;;) {
  1526. temp = I915_READ(fdi_rx_iir_reg);
  1527. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1528. if (temp & FDI_RX_SYMBOL_LOCK) {
  1529. I915_WRITE(fdi_rx_iir_reg,
  1530. temp | FDI_RX_SYMBOL_LOCK);
  1531. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1532. break;
  1533. }
  1534. tries++;
  1535. if (tries > 5) {
  1536. DRM_DEBUG_KMS("FDI train 2 fail!\n");
  1537. break;
  1538. }
  1539. }
  1540. DRM_DEBUG_KMS("FDI train done\n");
  1541. }
  1542. static int snb_b_fdi_train_param [] = {
  1543. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  1544. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  1545. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  1546. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  1547. };
  1548. /* The FDI link training functions for SNB/Cougarpoint. */
  1549. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  1550. {
  1551. struct drm_device *dev = crtc->dev;
  1552. struct drm_i915_private *dev_priv = dev->dev_private;
  1553. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1554. int pipe = intel_crtc->pipe;
  1555. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1556. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1557. int fdi_rx_iir_reg = (pipe == 0) ? FDI_RXA_IIR : FDI_RXB_IIR;
  1558. int fdi_rx_imr_reg = (pipe == 0) ? FDI_RXA_IMR : FDI_RXB_IMR;
  1559. u32 temp, i;
  1560. /* enable CPU FDI TX and PCH FDI RX */
  1561. temp = I915_READ(fdi_tx_reg);
  1562. temp |= FDI_TX_ENABLE;
  1563. temp &= ~(7 << 19);
  1564. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1565. temp &= ~FDI_LINK_TRAIN_NONE;
  1566. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1567. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1568. /* SNB-B */
  1569. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1570. I915_WRITE(fdi_tx_reg, temp);
  1571. I915_READ(fdi_tx_reg);
  1572. temp = I915_READ(fdi_rx_reg);
  1573. if (HAS_PCH_CPT(dev)) {
  1574. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1575. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1576. } else {
  1577. temp &= ~FDI_LINK_TRAIN_NONE;
  1578. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1579. }
  1580. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENABLE);
  1581. I915_READ(fdi_rx_reg);
  1582. udelay(150);
  1583. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  1584. for train result */
  1585. temp = I915_READ(fdi_rx_imr_reg);
  1586. temp &= ~FDI_RX_SYMBOL_LOCK;
  1587. temp &= ~FDI_RX_BIT_LOCK;
  1588. I915_WRITE(fdi_rx_imr_reg, temp);
  1589. I915_READ(fdi_rx_imr_reg);
  1590. udelay(150);
  1591. for (i = 0; i < 4; i++ ) {
  1592. temp = I915_READ(fdi_tx_reg);
  1593. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1594. temp |= snb_b_fdi_train_param[i];
  1595. I915_WRITE(fdi_tx_reg, temp);
  1596. udelay(500);
  1597. temp = I915_READ(fdi_rx_iir_reg);
  1598. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1599. if (temp & FDI_RX_BIT_LOCK) {
  1600. I915_WRITE(fdi_rx_iir_reg,
  1601. temp | FDI_RX_BIT_LOCK);
  1602. DRM_DEBUG_KMS("FDI train 1 done.\n");
  1603. break;
  1604. }
  1605. }
  1606. if (i == 4)
  1607. DRM_DEBUG_KMS("FDI train 1 fail!\n");
  1608. /* Train 2 */
  1609. temp = I915_READ(fdi_tx_reg);
  1610. temp &= ~FDI_LINK_TRAIN_NONE;
  1611. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1612. if (IS_GEN6(dev)) {
  1613. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1614. /* SNB-B */
  1615. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  1616. }
  1617. I915_WRITE(fdi_tx_reg, temp);
  1618. temp = I915_READ(fdi_rx_reg);
  1619. if (HAS_PCH_CPT(dev)) {
  1620. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1621. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  1622. } else {
  1623. temp &= ~FDI_LINK_TRAIN_NONE;
  1624. temp |= FDI_LINK_TRAIN_PATTERN_2;
  1625. }
  1626. I915_WRITE(fdi_rx_reg, temp);
  1627. udelay(150);
  1628. for (i = 0; i < 4; i++ ) {
  1629. temp = I915_READ(fdi_tx_reg);
  1630. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  1631. temp |= snb_b_fdi_train_param[i];
  1632. I915_WRITE(fdi_tx_reg, temp);
  1633. udelay(500);
  1634. temp = I915_READ(fdi_rx_iir_reg);
  1635. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  1636. if (temp & FDI_RX_SYMBOL_LOCK) {
  1637. I915_WRITE(fdi_rx_iir_reg,
  1638. temp | FDI_RX_SYMBOL_LOCK);
  1639. DRM_DEBUG_KMS("FDI train 2 done.\n");
  1640. break;
  1641. }
  1642. }
  1643. if (i == 4)
  1644. DRM_DEBUG_KMS("FDI train 2 fail!\n");
  1645. DRM_DEBUG_KMS("FDI train done.\n");
  1646. }
  1647. static void ironlake_crtc_dpms(struct drm_crtc *crtc, int mode)
  1648. {
  1649. struct drm_device *dev = crtc->dev;
  1650. struct drm_i915_private *dev_priv = dev->dev_private;
  1651. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1652. int pipe = intel_crtc->pipe;
  1653. int plane = intel_crtc->plane;
  1654. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  1655. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  1656. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  1657. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  1658. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  1659. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  1660. int transconf_reg = (pipe == 0) ? TRANSACONF : TRANSBCONF;
  1661. int pf_ctl_reg = (pipe == 0) ? PFA_CTL_1 : PFB_CTL_1;
  1662. int pf_win_size = (pipe == 0) ? PFA_WIN_SZ : PFB_WIN_SZ;
  1663. int pf_win_pos = (pipe == 0) ? PFA_WIN_POS : PFB_WIN_POS;
  1664. int cpu_htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  1665. int cpu_hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  1666. int cpu_hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  1667. int cpu_vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  1668. int cpu_vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  1669. int cpu_vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  1670. int trans_htot_reg = (pipe == 0) ? TRANS_HTOTAL_A : TRANS_HTOTAL_B;
  1671. int trans_hblank_reg = (pipe == 0) ? TRANS_HBLANK_A : TRANS_HBLANK_B;
  1672. int trans_hsync_reg = (pipe == 0) ? TRANS_HSYNC_A : TRANS_HSYNC_B;
  1673. int trans_vtot_reg = (pipe == 0) ? TRANS_VTOTAL_A : TRANS_VTOTAL_B;
  1674. int trans_vblank_reg = (pipe == 0) ? TRANS_VBLANK_A : TRANS_VBLANK_B;
  1675. int trans_vsync_reg = (pipe == 0) ? TRANS_VSYNC_A : TRANS_VSYNC_B;
  1676. int trans_dpll_sel = (pipe == 0) ? 0 : 1;
  1677. u32 temp;
  1678. int n;
  1679. u32 pipe_bpc;
  1680. temp = I915_READ(pipeconf_reg);
  1681. pipe_bpc = temp & PIPE_BPC_MASK;
  1682. /* XXX: When our outputs are all unaware of DPMS modes other than off
  1683. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  1684. */
  1685. switch (mode) {
  1686. case DRM_MODE_DPMS_ON:
  1687. case DRM_MODE_DPMS_STANDBY:
  1688. case DRM_MODE_DPMS_SUSPEND:
  1689. DRM_DEBUG_KMS("crtc %d dpms on\n", pipe);
  1690. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1691. temp = I915_READ(PCH_LVDS);
  1692. if ((temp & LVDS_PORT_EN) == 0) {
  1693. I915_WRITE(PCH_LVDS, temp | LVDS_PORT_EN);
  1694. POSTING_READ(PCH_LVDS);
  1695. }
  1696. }
  1697. if (HAS_eDP) {
  1698. /* enable eDP PLL */
  1699. ironlake_enable_pll_edp(crtc);
  1700. } else {
  1701. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  1702. temp = I915_READ(fdi_rx_reg);
  1703. /*
  1704. * make the BPC in FDI Rx be consistent with that in
  1705. * pipeconf reg.
  1706. */
  1707. temp &= ~(0x7 << 16);
  1708. temp |= (pipe_bpc << 11);
  1709. temp &= ~(7 << 19);
  1710. temp |= (intel_crtc->fdi_lanes - 1) << 19;
  1711. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
  1712. I915_READ(fdi_rx_reg);
  1713. udelay(200);
  1714. /* Switch from Rawclk to PCDclk */
  1715. temp = I915_READ(fdi_rx_reg);
  1716. I915_WRITE(fdi_rx_reg, temp | FDI_SEL_PCDCLK);
  1717. I915_READ(fdi_rx_reg);
  1718. udelay(200);
  1719. /* Enable CPU FDI TX PLL, always on for Ironlake */
  1720. temp = I915_READ(fdi_tx_reg);
  1721. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  1722. I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
  1723. I915_READ(fdi_tx_reg);
  1724. udelay(100);
  1725. }
  1726. }
  1727. /* Enable panel fitting for LVDS */
  1728. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1729. temp = I915_READ(pf_ctl_reg);
  1730. I915_WRITE(pf_ctl_reg, temp | PF_ENABLE | PF_FILTER_MED_3x3);
  1731. /* currently full aspect */
  1732. I915_WRITE(pf_win_pos, 0);
  1733. I915_WRITE(pf_win_size,
  1734. (dev_priv->panel_fixed_mode->hdisplay << 16) |
  1735. (dev_priv->panel_fixed_mode->vdisplay));
  1736. }
  1737. /* Enable CPU pipe */
  1738. temp = I915_READ(pipeconf_reg);
  1739. if ((temp & PIPEACONF_ENABLE) == 0) {
  1740. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  1741. I915_READ(pipeconf_reg);
  1742. udelay(100);
  1743. }
  1744. /* configure and enable CPU plane */
  1745. temp = I915_READ(dspcntr_reg);
  1746. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  1747. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  1748. /* Flush the plane changes */
  1749. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1750. }
  1751. if (!HAS_eDP) {
  1752. /* For PCH output, training FDI link */
  1753. if (IS_GEN6(dev))
  1754. gen6_fdi_link_train(crtc);
  1755. else
  1756. ironlake_fdi_link_train(crtc);
  1757. /* enable PCH DPLL */
  1758. temp = I915_READ(pch_dpll_reg);
  1759. if ((temp & DPLL_VCO_ENABLE) == 0) {
  1760. I915_WRITE(pch_dpll_reg, temp | DPLL_VCO_ENABLE);
  1761. I915_READ(pch_dpll_reg);
  1762. }
  1763. udelay(200);
  1764. if (HAS_PCH_CPT(dev)) {
  1765. /* Be sure PCH DPLL SEL is set */
  1766. temp = I915_READ(PCH_DPLL_SEL);
  1767. if (trans_dpll_sel == 0 &&
  1768. (temp & TRANSA_DPLL_ENABLE) == 0)
  1769. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  1770. else if (trans_dpll_sel == 1 &&
  1771. (temp & TRANSB_DPLL_ENABLE) == 0)
  1772. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1773. I915_WRITE(PCH_DPLL_SEL, temp);
  1774. I915_READ(PCH_DPLL_SEL);
  1775. }
  1776. /* set transcoder timing */
  1777. I915_WRITE(trans_htot_reg, I915_READ(cpu_htot_reg));
  1778. I915_WRITE(trans_hblank_reg, I915_READ(cpu_hblank_reg));
  1779. I915_WRITE(trans_hsync_reg, I915_READ(cpu_hsync_reg));
  1780. I915_WRITE(trans_vtot_reg, I915_READ(cpu_vtot_reg));
  1781. I915_WRITE(trans_vblank_reg, I915_READ(cpu_vblank_reg));
  1782. I915_WRITE(trans_vsync_reg, I915_READ(cpu_vsync_reg));
  1783. /* enable normal train */
  1784. temp = I915_READ(fdi_tx_reg);
  1785. temp &= ~FDI_LINK_TRAIN_NONE;
  1786. I915_WRITE(fdi_tx_reg, temp | FDI_LINK_TRAIN_NONE |
  1787. FDI_TX_ENHANCE_FRAME_ENABLE);
  1788. I915_READ(fdi_tx_reg);
  1789. temp = I915_READ(fdi_rx_reg);
  1790. if (HAS_PCH_CPT(dev)) {
  1791. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1792. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  1793. } else {
  1794. temp &= ~FDI_LINK_TRAIN_NONE;
  1795. temp |= FDI_LINK_TRAIN_NONE;
  1796. }
  1797. I915_WRITE(fdi_rx_reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  1798. I915_READ(fdi_rx_reg);
  1799. /* wait one idle pattern time */
  1800. udelay(100);
  1801. /* For PCH DP, enable TRANS_DP_CTL */
  1802. if (HAS_PCH_CPT(dev) &&
  1803. intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
  1804. int trans_dp_ctl = (pipe == 0) ? TRANS_DP_CTL_A : TRANS_DP_CTL_B;
  1805. int reg;
  1806. reg = I915_READ(trans_dp_ctl);
  1807. reg &= ~TRANS_DP_PORT_SEL_MASK;
  1808. reg = TRANS_DP_OUTPUT_ENABLE |
  1809. TRANS_DP_ENH_FRAMING |
  1810. TRANS_DP_VSYNC_ACTIVE_HIGH |
  1811. TRANS_DP_HSYNC_ACTIVE_HIGH;
  1812. switch (intel_trans_dp_port_sel(crtc)) {
  1813. case PCH_DP_B:
  1814. reg |= TRANS_DP_PORT_SEL_B;
  1815. break;
  1816. case PCH_DP_C:
  1817. reg |= TRANS_DP_PORT_SEL_C;
  1818. break;
  1819. case PCH_DP_D:
  1820. reg |= TRANS_DP_PORT_SEL_D;
  1821. break;
  1822. default:
  1823. DRM_DEBUG_KMS("Wrong PCH DP port return. Guess port B\n");
  1824. reg |= TRANS_DP_PORT_SEL_B;
  1825. break;
  1826. }
  1827. I915_WRITE(trans_dp_ctl, reg);
  1828. POSTING_READ(trans_dp_ctl);
  1829. }
  1830. /* enable PCH transcoder */
  1831. temp = I915_READ(transconf_reg);
  1832. /*
  1833. * make the BPC in transcoder be consistent with
  1834. * that in pipeconf reg.
  1835. */
  1836. temp &= ~PIPE_BPC_MASK;
  1837. temp |= pipe_bpc;
  1838. I915_WRITE(transconf_reg, temp | TRANS_ENABLE);
  1839. I915_READ(transconf_reg);
  1840. while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) == 0)
  1841. ;
  1842. }
  1843. intel_crtc_load_lut(crtc);
  1844. break;
  1845. case DRM_MODE_DPMS_OFF:
  1846. DRM_DEBUG_KMS("crtc %d dpms off\n", pipe);
  1847. drm_vblank_off(dev, pipe);
  1848. /* Disable display plane */
  1849. temp = I915_READ(dspcntr_reg);
  1850. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  1851. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  1852. /* Flush the plane changes */
  1853. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  1854. I915_READ(dspbase_reg);
  1855. }
  1856. i915_disable_vga(dev);
  1857. /* disable cpu pipe, disable after all planes disabled */
  1858. temp = I915_READ(pipeconf_reg);
  1859. if ((temp & PIPEACONF_ENABLE) != 0) {
  1860. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  1861. I915_READ(pipeconf_reg);
  1862. n = 0;
  1863. /* wait for cpu pipe off, pipe state */
  1864. while ((I915_READ(pipeconf_reg) & I965_PIPECONF_ACTIVE) != 0) {
  1865. n++;
  1866. if (n < 60) {
  1867. udelay(500);
  1868. continue;
  1869. } else {
  1870. DRM_DEBUG_KMS("pipe %d off delay\n",
  1871. pipe);
  1872. break;
  1873. }
  1874. }
  1875. } else
  1876. DRM_DEBUG_KMS("crtc %d is disabled\n", pipe);
  1877. udelay(100);
  1878. /* Disable PF */
  1879. temp = I915_READ(pf_ctl_reg);
  1880. if ((temp & PF_ENABLE) != 0) {
  1881. I915_WRITE(pf_ctl_reg, temp & ~PF_ENABLE);
  1882. I915_READ(pf_ctl_reg);
  1883. }
  1884. I915_WRITE(pf_win_size, 0);
  1885. POSTING_READ(pf_win_size);
  1886. /* disable CPU FDI tx and PCH FDI rx */
  1887. temp = I915_READ(fdi_tx_reg);
  1888. I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_ENABLE);
  1889. I915_READ(fdi_tx_reg);
  1890. temp = I915_READ(fdi_rx_reg);
  1891. /* BPC in FDI rx is consistent with that in pipeconf */
  1892. temp &= ~(0x07 << 16);
  1893. temp |= (pipe_bpc << 11);
  1894. I915_WRITE(fdi_rx_reg, temp & ~FDI_RX_ENABLE);
  1895. I915_READ(fdi_rx_reg);
  1896. udelay(100);
  1897. /* still set train pattern 1 */
  1898. temp = I915_READ(fdi_tx_reg);
  1899. temp &= ~FDI_LINK_TRAIN_NONE;
  1900. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1901. I915_WRITE(fdi_tx_reg, temp);
  1902. POSTING_READ(fdi_tx_reg);
  1903. temp = I915_READ(fdi_rx_reg);
  1904. if (HAS_PCH_CPT(dev)) {
  1905. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  1906. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  1907. } else {
  1908. temp &= ~FDI_LINK_TRAIN_NONE;
  1909. temp |= FDI_LINK_TRAIN_PATTERN_1;
  1910. }
  1911. I915_WRITE(fdi_rx_reg, temp);
  1912. POSTING_READ(fdi_rx_reg);
  1913. udelay(100);
  1914. if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  1915. temp = I915_READ(PCH_LVDS);
  1916. I915_WRITE(PCH_LVDS, temp & ~LVDS_PORT_EN);
  1917. I915_READ(PCH_LVDS);
  1918. udelay(100);
  1919. }
  1920. /* disable PCH transcoder */
  1921. temp = I915_READ(transconf_reg);
  1922. if ((temp & TRANS_ENABLE) != 0) {
  1923. I915_WRITE(transconf_reg, temp & ~TRANS_ENABLE);
  1924. I915_READ(transconf_reg);
  1925. n = 0;
  1926. /* wait for PCH transcoder off, transcoder state */
  1927. while ((I915_READ(transconf_reg) & TRANS_STATE_ENABLE) != 0) {
  1928. n++;
  1929. if (n < 60) {
  1930. udelay(500);
  1931. continue;
  1932. } else {
  1933. DRM_DEBUG_KMS("transcoder %d off "
  1934. "delay\n", pipe);
  1935. break;
  1936. }
  1937. }
  1938. }
  1939. temp = I915_READ(transconf_reg);
  1940. /* BPC in transcoder is consistent with that in pipeconf */
  1941. temp &= ~PIPE_BPC_MASK;
  1942. temp |= pipe_bpc;
  1943. I915_WRITE(transconf_reg, temp);
  1944. I915_READ(transconf_reg);
  1945. udelay(100);
  1946. if (HAS_PCH_CPT(dev)) {
  1947. /* disable TRANS_DP_CTL */
  1948. int trans_dp_ctl = (pipe == 0) ? TRANS_DP_CTL_A : TRANS_DP_CTL_B;
  1949. int reg;
  1950. reg = I915_READ(trans_dp_ctl);
  1951. reg &= ~(TRANS_DP_OUTPUT_ENABLE | TRANS_DP_PORT_SEL_MASK);
  1952. I915_WRITE(trans_dp_ctl, reg);
  1953. POSTING_READ(trans_dp_ctl);
  1954. /* disable DPLL_SEL */
  1955. temp = I915_READ(PCH_DPLL_SEL);
  1956. if (trans_dpll_sel == 0)
  1957. temp &= ~(TRANSA_DPLL_ENABLE | TRANSA_DPLLB_SEL);
  1958. else
  1959. temp &= ~(TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  1960. I915_WRITE(PCH_DPLL_SEL, temp);
  1961. I915_READ(PCH_DPLL_SEL);
  1962. }
  1963. /* disable PCH DPLL */
  1964. temp = I915_READ(pch_dpll_reg);
  1965. I915_WRITE(pch_dpll_reg, temp & ~DPLL_VCO_ENABLE);
  1966. I915_READ(pch_dpll_reg);
  1967. if (HAS_eDP) {
  1968. ironlake_disable_pll_edp(crtc);
  1969. }
  1970. /* Switch from PCDclk to Rawclk */
  1971. temp = I915_READ(fdi_rx_reg);
  1972. temp &= ~FDI_SEL_PCDCLK;
  1973. I915_WRITE(fdi_rx_reg, temp);
  1974. I915_READ(fdi_rx_reg);
  1975. /* Disable CPU FDI TX PLL */
  1976. temp = I915_READ(fdi_tx_reg);
  1977. I915_WRITE(fdi_tx_reg, temp & ~FDI_TX_PLL_ENABLE);
  1978. I915_READ(fdi_tx_reg);
  1979. udelay(100);
  1980. temp = I915_READ(fdi_rx_reg);
  1981. temp &= ~FDI_RX_PLL_ENABLE;
  1982. I915_WRITE(fdi_rx_reg, temp);
  1983. I915_READ(fdi_rx_reg);
  1984. /* Wait for the clocks to turn off. */
  1985. udelay(100);
  1986. break;
  1987. }
  1988. }
  1989. static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
  1990. {
  1991. struct intel_overlay *overlay;
  1992. int ret;
  1993. if (!enable && intel_crtc->overlay) {
  1994. overlay = intel_crtc->overlay;
  1995. mutex_lock(&overlay->dev->struct_mutex);
  1996. for (;;) {
  1997. ret = intel_overlay_switch_off(overlay);
  1998. if (ret == 0)
  1999. break;
  2000. ret = intel_overlay_recover_from_interrupt(overlay, 0);
  2001. if (ret != 0) {
  2002. /* overlay doesn't react anymore. Usually
  2003. * results in a black screen and an unkillable
  2004. * X server. */
  2005. BUG();
  2006. overlay->hw_wedged = HW_WEDGED;
  2007. break;
  2008. }
  2009. }
  2010. mutex_unlock(&overlay->dev->struct_mutex);
  2011. }
  2012. /* Let userspace switch the overlay on again. In most cases userspace
  2013. * has to recompute where to put it anyway. */
  2014. return;
  2015. }
  2016. static void i9xx_crtc_dpms(struct drm_crtc *crtc, int mode)
  2017. {
  2018. struct drm_device *dev = crtc->dev;
  2019. struct drm_i915_private *dev_priv = dev->dev_private;
  2020. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2021. int pipe = intel_crtc->pipe;
  2022. int plane = intel_crtc->plane;
  2023. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  2024. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  2025. int dspbase_reg = (plane == 0) ? DSPAADDR : DSPBADDR;
  2026. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  2027. u32 temp;
  2028. /* XXX: When our outputs are all unaware of DPMS modes other than off
  2029. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  2030. */
  2031. switch (mode) {
  2032. case DRM_MODE_DPMS_ON:
  2033. case DRM_MODE_DPMS_STANDBY:
  2034. case DRM_MODE_DPMS_SUSPEND:
  2035. intel_update_watermarks(dev);
  2036. /* Enable the DPLL */
  2037. temp = I915_READ(dpll_reg);
  2038. if ((temp & DPLL_VCO_ENABLE) == 0) {
  2039. I915_WRITE(dpll_reg, temp);
  2040. I915_READ(dpll_reg);
  2041. /* Wait for the clocks to stabilize. */
  2042. udelay(150);
  2043. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  2044. I915_READ(dpll_reg);
  2045. /* Wait for the clocks to stabilize. */
  2046. udelay(150);
  2047. I915_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  2048. I915_READ(dpll_reg);
  2049. /* Wait for the clocks to stabilize. */
  2050. udelay(150);
  2051. }
  2052. /* Enable the pipe */
  2053. temp = I915_READ(pipeconf_reg);
  2054. if ((temp & PIPEACONF_ENABLE) == 0)
  2055. I915_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  2056. /* Enable the plane */
  2057. temp = I915_READ(dspcntr_reg);
  2058. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  2059. I915_WRITE(dspcntr_reg, temp | DISPLAY_PLANE_ENABLE);
  2060. /* Flush the plane changes */
  2061. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  2062. }
  2063. intel_crtc_load_lut(crtc);
  2064. if ((IS_I965G(dev) || plane == 0))
  2065. intel_update_fbc(crtc, &crtc->mode);
  2066. /* Give the overlay scaler a chance to enable if it's on this pipe */
  2067. intel_crtc_dpms_overlay(intel_crtc, true);
  2068. break;
  2069. case DRM_MODE_DPMS_OFF:
  2070. intel_update_watermarks(dev);
  2071. /* Give the overlay scaler a chance to disable if it's on this pipe */
  2072. intel_crtc_dpms_overlay(intel_crtc, false);
  2073. drm_vblank_off(dev, pipe);
  2074. if (dev_priv->cfb_plane == plane &&
  2075. dev_priv->display.disable_fbc)
  2076. dev_priv->display.disable_fbc(dev);
  2077. /* Disable the VGA plane that we never use */
  2078. i915_disable_vga(dev);
  2079. /* Disable display plane */
  2080. temp = I915_READ(dspcntr_reg);
  2081. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  2082. I915_WRITE(dspcntr_reg, temp & ~DISPLAY_PLANE_ENABLE);
  2083. /* Flush the plane changes */
  2084. I915_WRITE(dspbase_reg, I915_READ(dspbase_reg));
  2085. I915_READ(dspbase_reg);
  2086. }
  2087. if (!IS_I9XX(dev)) {
  2088. /* Wait for vblank for the disable to take effect */
  2089. intel_wait_for_vblank(dev);
  2090. }
  2091. /* Don't disable pipe A or pipe A PLLs if needed */
  2092. if (pipeconf_reg == PIPEACONF &&
  2093. (dev_priv->quirks & QUIRK_PIPEA_FORCE))
  2094. goto skip_pipe_off;
  2095. /* Next, disable display pipes */
  2096. temp = I915_READ(pipeconf_reg);
  2097. if ((temp & PIPEACONF_ENABLE) != 0) {
  2098. I915_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  2099. I915_READ(pipeconf_reg);
  2100. }
  2101. /* Wait for vblank for the disable to take effect. */
  2102. intel_wait_for_vblank(dev);
  2103. temp = I915_READ(dpll_reg);
  2104. if ((temp & DPLL_VCO_ENABLE) != 0) {
  2105. I915_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  2106. I915_READ(dpll_reg);
  2107. }
  2108. skip_pipe_off:
  2109. /* Wait for the clocks to turn off. */
  2110. udelay(150);
  2111. break;
  2112. }
  2113. }
  2114. /**
  2115. * Sets the power management mode of the pipe and plane.
  2116. *
  2117. * This code should probably grow support for turning the cursor off and back
  2118. * on appropriately at the same time as we're turning the pipe off/on.
  2119. */
  2120. static void intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  2121. {
  2122. struct drm_device *dev = crtc->dev;
  2123. struct drm_i915_private *dev_priv = dev->dev_private;
  2124. struct drm_i915_master_private *master_priv;
  2125. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2126. int pipe = intel_crtc->pipe;
  2127. bool enabled;
  2128. dev_priv->display.dpms(crtc, mode);
  2129. intel_crtc->dpms_mode = mode;
  2130. if (!dev->primary->master)
  2131. return;
  2132. master_priv = dev->primary->master->driver_priv;
  2133. if (!master_priv->sarea_priv)
  2134. return;
  2135. enabled = crtc->enabled && mode != DRM_MODE_DPMS_OFF;
  2136. switch (pipe) {
  2137. case 0:
  2138. master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
  2139. master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
  2140. break;
  2141. case 1:
  2142. master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
  2143. master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
  2144. break;
  2145. default:
  2146. DRM_ERROR("Can't update pipe %d in SAREA\n", pipe);
  2147. break;
  2148. }
  2149. }
  2150. static void intel_crtc_prepare (struct drm_crtc *crtc)
  2151. {
  2152. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2153. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  2154. }
  2155. static void intel_crtc_commit (struct drm_crtc *crtc)
  2156. {
  2157. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  2158. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  2159. }
  2160. void intel_encoder_prepare (struct drm_encoder *encoder)
  2161. {
  2162. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2163. /* lvds has its own version of prepare see intel_lvds_prepare */
  2164. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  2165. }
  2166. void intel_encoder_commit (struct drm_encoder *encoder)
  2167. {
  2168. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  2169. /* lvds has its own version of commit see intel_lvds_commit */
  2170. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  2171. }
  2172. static bool intel_crtc_mode_fixup(struct drm_crtc *crtc,
  2173. struct drm_display_mode *mode,
  2174. struct drm_display_mode *adjusted_mode)
  2175. {
  2176. struct drm_device *dev = crtc->dev;
  2177. if (HAS_PCH_SPLIT(dev)) {
  2178. /* FDI link clock is fixed at 2.7G */
  2179. if (mode->clock * 3 > 27000 * 4)
  2180. return MODE_CLOCK_HIGH;
  2181. }
  2182. return true;
  2183. }
  2184. static int i945_get_display_clock_speed(struct drm_device *dev)
  2185. {
  2186. return 400000;
  2187. }
  2188. static int i915_get_display_clock_speed(struct drm_device *dev)
  2189. {
  2190. return 333000;
  2191. }
  2192. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  2193. {
  2194. return 200000;
  2195. }
  2196. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  2197. {
  2198. u16 gcfgc = 0;
  2199. pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
  2200. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  2201. return 133000;
  2202. else {
  2203. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  2204. case GC_DISPLAY_CLOCK_333_MHZ:
  2205. return 333000;
  2206. default:
  2207. case GC_DISPLAY_CLOCK_190_200_MHZ:
  2208. return 190000;
  2209. }
  2210. }
  2211. }
  2212. static int i865_get_display_clock_speed(struct drm_device *dev)
  2213. {
  2214. return 266000;
  2215. }
  2216. static int i855_get_display_clock_speed(struct drm_device *dev)
  2217. {
  2218. u16 hpllcc = 0;
  2219. /* Assume that the hardware is in the high speed state. This
  2220. * should be the default.
  2221. */
  2222. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  2223. case GC_CLOCK_133_200:
  2224. case GC_CLOCK_100_200:
  2225. return 200000;
  2226. case GC_CLOCK_166_250:
  2227. return 250000;
  2228. case GC_CLOCK_100_133:
  2229. return 133000;
  2230. }
  2231. /* Shouldn't happen */
  2232. return 0;
  2233. }
  2234. static int i830_get_display_clock_speed(struct drm_device *dev)
  2235. {
  2236. return 133000;
  2237. }
  2238. /**
  2239. * Return the pipe currently connected to the panel fitter,
  2240. * or -1 if the panel fitter is not present or not in use
  2241. */
  2242. int intel_panel_fitter_pipe (struct drm_device *dev)
  2243. {
  2244. struct drm_i915_private *dev_priv = dev->dev_private;
  2245. u32 pfit_control;
  2246. /* i830 doesn't have a panel fitter */
  2247. if (IS_I830(dev))
  2248. return -1;
  2249. pfit_control = I915_READ(PFIT_CONTROL);
  2250. /* See if the panel fitter is in use */
  2251. if ((pfit_control & PFIT_ENABLE) == 0)
  2252. return -1;
  2253. /* 965 can place panel fitter on either pipe */
  2254. if (IS_I965G(dev))
  2255. return (pfit_control >> 29) & 0x3;
  2256. /* older chips can only use pipe 1 */
  2257. return 1;
  2258. }
  2259. struct fdi_m_n {
  2260. u32 tu;
  2261. u32 gmch_m;
  2262. u32 gmch_n;
  2263. u32 link_m;
  2264. u32 link_n;
  2265. };
  2266. static void
  2267. fdi_reduce_ratio(u32 *num, u32 *den)
  2268. {
  2269. while (*num > 0xffffff || *den > 0xffffff) {
  2270. *num >>= 1;
  2271. *den >>= 1;
  2272. }
  2273. }
  2274. #define DATA_N 0x800000
  2275. #define LINK_N 0x80000
  2276. static void
  2277. ironlake_compute_m_n(int bits_per_pixel, int nlanes, int pixel_clock,
  2278. int link_clock, struct fdi_m_n *m_n)
  2279. {
  2280. u64 temp;
  2281. m_n->tu = 64; /* default size */
  2282. temp = (u64) DATA_N * pixel_clock;
  2283. temp = div_u64(temp, link_clock);
  2284. m_n->gmch_m = div_u64(temp * bits_per_pixel, nlanes);
  2285. m_n->gmch_m >>= 3; /* convert to bytes_per_pixel */
  2286. m_n->gmch_n = DATA_N;
  2287. fdi_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  2288. temp = (u64) LINK_N * pixel_clock;
  2289. m_n->link_m = div_u64(temp, link_clock);
  2290. m_n->link_n = LINK_N;
  2291. fdi_reduce_ratio(&m_n->link_m, &m_n->link_n);
  2292. }
  2293. struct intel_watermark_params {
  2294. unsigned long fifo_size;
  2295. unsigned long max_wm;
  2296. unsigned long default_wm;
  2297. unsigned long guard_size;
  2298. unsigned long cacheline_size;
  2299. };
  2300. /* Pineview has different values for various configs */
  2301. static struct intel_watermark_params pineview_display_wm = {
  2302. PINEVIEW_DISPLAY_FIFO,
  2303. PINEVIEW_MAX_WM,
  2304. PINEVIEW_DFT_WM,
  2305. PINEVIEW_GUARD_WM,
  2306. PINEVIEW_FIFO_LINE_SIZE
  2307. };
  2308. static struct intel_watermark_params pineview_display_hplloff_wm = {
  2309. PINEVIEW_DISPLAY_FIFO,
  2310. PINEVIEW_MAX_WM,
  2311. PINEVIEW_DFT_HPLLOFF_WM,
  2312. PINEVIEW_GUARD_WM,
  2313. PINEVIEW_FIFO_LINE_SIZE
  2314. };
  2315. static struct intel_watermark_params pineview_cursor_wm = {
  2316. PINEVIEW_CURSOR_FIFO,
  2317. PINEVIEW_CURSOR_MAX_WM,
  2318. PINEVIEW_CURSOR_DFT_WM,
  2319. PINEVIEW_CURSOR_GUARD_WM,
  2320. PINEVIEW_FIFO_LINE_SIZE,
  2321. };
  2322. static struct intel_watermark_params pineview_cursor_hplloff_wm = {
  2323. PINEVIEW_CURSOR_FIFO,
  2324. PINEVIEW_CURSOR_MAX_WM,
  2325. PINEVIEW_CURSOR_DFT_WM,
  2326. PINEVIEW_CURSOR_GUARD_WM,
  2327. PINEVIEW_FIFO_LINE_SIZE
  2328. };
  2329. static struct intel_watermark_params g4x_wm_info = {
  2330. G4X_FIFO_SIZE,
  2331. G4X_MAX_WM,
  2332. G4X_MAX_WM,
  2333. 2,
  2334. G4X_FIFO_LINE_SIZE,
  2335. };
  2336. static struct intel_watermark_params i945_wm_info = {
  2337. I945_FIFO_SIZE,
  2338. I915_MAX_WM,
  2339. 1,
  2340. 2,
  2341. I915_FIFO_LINE_SIZE
  2342. };
  2343. static struct intel_watermark_params i915_wm_info = {
  2344. I915_FIFO_SIZE,
  2345. I915_MAX_WM,
  2346. 1,
  2347. 2,
  2348. I915_FIFO_LINE_SIZE
  2349. };
  2350. static struct intel_watermark_params i855_wm_info = {
  2351. I855GM_FIFO_SIZE,
  2352. I915_MAX_WM,
  2353. 1,
  2354. 2,
  2355. I830_FIFO_LINE_SIZE
  2356. };
  2357. static struct intel_watermark_params i830_wm_info = {
  2358. I830_FIFO_SIZE,
  2359. I915_MAX_WM,
  2360. 1,
  2361. 2,
  2362. I830_FIFO_LINE_SIZE
  2363. };
  2364. static struct intel_watermark_params ironlake_display_wm_info = {
  2365. ILK_DISPLAY_FIFO,
  2366. ILK_DISPLAY_MAXWM,
  2367. ILK_DISPLAY_DFTWM,
  2368. 2,
  2369. ILK_FIFO_LINE_SIZE
  2370. };
  2371. static struct intel_watermark_params ironlake_display_srwm_info = {
  2372. ILK_DISPLAY_SR_FIFO,
  2373. ILK_DISPLAY_MAX_SRWM,
  2374. ILK_DISPLAY_DFT_SRWM,
  2375. 2,
  2376. ILK_FIFO_LINE_SIZE
  2377. };
  2378. static struct intel_watermark_params ironlake_cursor_srwm_info = {
  2379. ILK_CURSOR_SR_FIFO,
  2380. ILK_CURSOR_MAX_SRWM,
  2381. ILK_CURSOR_DFT_SRWM,
  2382. 2,
  2383. ILK_FIFO_LINE_SIZE
  2384. };
  2385. /**
  2386. * intel_calculate_wm - calculate watermark level
  2387. * @clock_in_khz: pixel clock
  2388. * @wm: chip FIFO params
  2389. * @pixel_size: display pixel size
  2390. * @latency_ns: memory latency for the platform
  2391. *
  2392. * Calculate the watermark level (the level at which the display plane will
  2393. * start fetching from memory again). Each chip has a different display
  2394. * FIFO size and allocation, so the caller needs to figure that out and pass
  2395. * in the correct intel_watermark_params structure.
  2396. *
  2397. * As the pixel clock runs, the FIFO will be drained at a rate that depends
  2398. * on the pixel size. When it reaches the watermark level, it'll start
  2399. * fetching FIFO line sized based chunks from memory until the FIFO fills
  2400. * past the watermark point. If the FIFO drains completely, a FIFO underrun
  2401. * will occur, and a display engine hang could result.
  2402. */
  2403. static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
  2404. struct intel_watermark_params *wm,
  2405. int pixel_size,
  2406. unsigned long latency_ns)
  2407. {
  2408. long entries_required, wm_size;
  2409. /*
  2410. * Note: we need to make sure we don't overflow for various clock &
  2411. * latency values.
  2412. * clocks go from a few thousand to several hundred thousand.
  2413. * latency is usually a few thousand
  2414. */
  2415. entries_required = ((clock_in_khz / 1000) * pixel_size * latency_ns) /
  2416. 1000;
  2417. entries_required /= wm->cacheline_size;
  2418. DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries_required);
  2419. wm_size = wm->fifo_size - (entries_required + wm->guard_size);
  2420. DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
  2421. /* Don't promote wm_size to unsigned... */
  2422. if (wm_size > (long)wm->max_wm)
  2423. wm_size = wm->max_wm;
  2424. if (wm_size <= 0)
  2425. wm_size = wm->default_wm;
  2426. return wm_size;
  2427. }
  2428. struct cxsr_latency {
  2429. int is_desktop;
  2430. int is_ddr3;
  2431. unsigned long fsb_freq;
  2432. unsigned long mem_freq;
  2433. unsigned long display_sr;
  2434. unsigned long display_hpll_disable;
  2435. unsigned long cursor_sr;
  2436. unsigned long cursor_hpll_disable;
  2437. };
  2438. static struct cxsr_latency cxsr_latency_table[] = {
  2439. {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
  2440. {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
  2441. {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
  2442. {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
  2443. {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
  2444. {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
  2445. {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
  2446. {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
  2447. {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
  2448. {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
  2449. {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
  2450. {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
  2451. {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
  2452. {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
  2453. {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
  2454. {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
  2455. {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
  2456. {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
  2457. {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
  2458. {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
  2459. {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
  2460. {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
  2461. {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
  2462. {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
  2463. {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
  2464. {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
  2465. {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
  2466. {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
  2467. {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
  2468. {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
  2469. };
  2470. static struct cxsr_latency *intel_get_cxsr_latency(int is_desktop, int is_ddr3,
  2471. int fsb, int mem)
  2472. {
  2473. int i;
  2474. struct cxsr_latency *latency;
  2475. if (fsb == 0 || mem == 0)
  2476. return NULL;
  2477. for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
  2478. latency = &cxsr_latency_table[i];
  2479. if (is_desktop == latency->is_desktop &&
  2480. is_ddr3 == latency->is_ddr3 &&
  2481. fsb == latency->fsb_freq && mem == latency->mem_freq)
  2482. return latency;
  2483. }
  2484. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2485. return NULL;
  2486. }
  2487. static void pineview_disable_cxsr(struct drm_device *dev)
  2488. {
  2489. struct drm_i915_private *dev_priv = dev->dev_private;
  2490. u32 reg;
  2491. /* deactivate cxsr */
  2492. reg = I915_READ(DSPFW3);
  2493. reg &= ~(PINEVIEW_SELF_REFRESH_EN);
  2494. I915_WRITE(DSPFW3, reg);
  2495. DRM_INFO("Big FIFO is disabled\n");
  2496. }
  2497. /*
  2498. * Latency for FIFO fetches is dependent on several factors:
  2499. * - memory configuration (speed, channels)
  2500. * - chipset
  2501. * - current MCH state
  2502. * It can be fairly high in some situations, so here we assume a fairly
  2503. * pessimal value. It's a tradeoff between extra memory fetches (if we
  2504. * set this value too high, the FIFO will fetch frequently to stay full)
  2505. * and power consumption (set it too low to save power and we might see
  2506. * FIFO underruns and display "flicker").
  2507. *
  2508. * A value of 5us seems to be a good balance; safe for very low end
  2509. * platforms but not overly aggressive on lower latency configs.
  2510. */
  2511. static const int latency_ns = 5000;
  2512. static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
  2513. {
  2514. struct drm_i915_private *dev_priv = dev->dev_private;
  2515. uint32_t dsparb = I915_READ(DSPARB);
  2516. int size;
  2517. if (plane == 0)
  2518. size = dsparb & 0x7f;
  2519. else
  2520. size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) -
  2521. (dsparb & 0x7f);
  2522. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2523. plane ? "B" : "A", size);
  2524. return size;
  2525. }
  2526. static int i85x_get_fifo_size(struct drm_device *dev, int plane)
  2527. {
  2528. struct drm_i915_private *dev_priv = dev->dev_private;
  2529. uint32_t dsparb = I915_READ(DSPARB);
  2530. int size;
  2531. if (plane == 0)
  2532. size = dsparb & 0x1ff;
  2533. else
  2534. size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) -
  2535. (dsparb & 0x1ff);
  2536. size >>= 1; /* Convert to cachelines */
  2537. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2538. plane ? "B" : "A", size);
  2539. return size;
  2540. }
  2541. static int i845_get_fifo_size(struct drm_device *dev, int plane)
  2542. {
  2543. struct drm_i915_private *dev_priv = dev->dev_private;
  2544. uint32_t dsparb = I915_READ(DSPARB);
  2545. int size;
  2546. size = dsparb & 0x7f;
  2547. size >>= 2; /* Convert to cachelines */
  2548. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2549. plane ? "B" : "A",
  2550. size);
  2551. return size;
  2552. }
  2553. static int i830_get_fifo_size(struct drm_device *dev, int plane)
  2554. {
  2555. struct drm_i915_private *dev_priv = dev->dev_private;
  2556. uint32_t dsparb = I915_READ(DSPARB);
  2557. int size;
  2558. size = dsparb & 0x7f;
  2559. size >>= 1; /* Convert to cachelines */
  2560. DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
  2561. plane ? "B" : "A", size);
  2562. return size;
  2563. }
  2564. static void pineview_update_wm(struct drm_device *dev, int planea_clock,
  2565. int planeb_clock, int sr_hdisplay, int pixel_size)
  2566. {
  2567. struct drm_i915_private *dev_priv = dev->dev_private;
  2568. u32 reg;
  2569. unsigned long wm;
  2570. struct cxsr_latency *latency;
  2571. int sr_clock;
  2572. latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
  2573. dev_priv->fsb_freq, dev_priv->mem_freq);
  2574. if (!latency) {
  2575. DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
  2576. pineview_disable_cxsr(dev);
  2577. return;
  2578. }
  2579. if (!planea_clock || !planeb_clock) {
  2580. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2581. /* Display SR */
  2582. wm = intel_calculate_wm(sr_clock, &pineview_display_wm,
  2583. pixel_size, latency->display_sr);
  2584. reg = I915_READ(DSPFW1);
  2585. reg &= ~DSPFW_SR_MASK;
  2586. reg |= wm << DSPFW_SR_SHIFT;
  2587. I915_WRITE(DSPFW1, reg);
  2588. DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
  2589. /* cursor SR */
  2590. wm = intel_calculate_wm(sr_clock, &pineview_cursor_wm,
  2591. pixel_size, latency->cursor_sr);
  2592. reg = I915_READ(DSPFW3);
  2593. reg &= ~DSPFW_CURSOR_SR_MASK;
  2594. reg |= (wm & 0x3f) << DSPFW_CURSOR_SR_SHIFT;
  2595. I915_WRITE(DSPFW3, reg);
  2596. /* Display HPLL off SR */
  2597. wm = intel_calculate_wm(sr_clock, &pineview_display_hplloff_wm,
  2598. pixel_size, latency->display_hpll_disable);
  2599. reg = I915_READ(DSPFW3);
  2600. reg &= ~DSPFW_HPLL_SR_MASK;
  2601. reg |= wm & DSPFW_HPLL_SR_MASK;
  2602. I915_WRITE(DSPFW3, reg);
  2603. /* cursor HPLL off SR */
  2604. wm = intel_calculate_wm(sr_clock, &pineview_cursor_hplloff_wm,
  2605. pixel_size, latency->cursor_hpll_disable);
  2606. reg = I915_READ(DSPFW3);
  2607. reg &= ~DSPFW_HPLL_CURSOR_MASK;
  2608. reg |= (wm & 0x3f) << DSPFW_HPLL_CURSOR_SHIFT;
  2609. I915_WRITE(DSPFW3, reg);
  2610. DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
  2611. /* activate cxsr */
  2612. reg = I915_READ(DSPFW3);
  2613. reg |= PINEVIEW_SELF_REFRESH_EN;
  2614. I915_WRITE(DSPFW3, reg);
  2615. DRM_DEBUG_KMS("Self-refresh is enabled\n");
  2616. } else {
  2617. pineview_disable_cxsr(dev);
  2618. DRM_DEBUG_KMS("Self-refresh is disabled\n");
  2619. }
  2620. }
  2621. static void g4x_update_wm(struct drm_device *dev, int planea_clock,
  2622. int planeb_clock, int sr_hdisplay, int pixel_size)
  2623. {
  2624. struct drm_i915_private *dev_priv = dev->dev_private;
  2625. int total_size, cacheline_size;
  2626. int planea_wm, planeb_wm, cursora_wm, cursorb_wm, cursor_sr;
  2627. struct intel_watermark_params planea_params, planeb_params;
  2628. unsigned long line_time_us;
  2629. int sr_clock, sr_entries = 0, entries_required;
  2630. /* Create copies of the base settings for each pipe */
  2631. planea_params = planeb_params = g4x_wm_info;
  2632. /* Grab a couple of global values before we overwrite them */
  2633. total_size = planea_params.fifo_size;
  2634. cacheline_size = planea_params.cacheline_size;
  2635. /*
  2636. * Note: we need to make sure we don't overflow for various clock &
  2637. * latency values.
  2638. * clocks go from a few thousand to several hundred thousand.
  2639. * latency is usually a few thousand
  2640. */
  2641. entries_required = ((planea_clock / 1000) * pixel_size * latency_ns) /
  2642. 1000;
  2643. entries_required /= G4X_FIFO_LINE_SIZE;
  2644. planea_wm = entries_required + planea_params.guard_size;
  2645. entries_required = ((planeb_clock / 1000) * pixel_size * latency_ns) /
  2646. 1000;
  2647. entries_required /= G4X_FIFO_LINE_SIZE;
  2648. planeb_wm = entries_required + planeb_params.guard_size;
  2649. cursora_wm = cursorb_wm = 16;
  2650. cursor_sr = 32;
  2651. DRM_DEBUG("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2652. /* Calc sr entries for one plane configs */
  2653. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2654. /* self-refresh has much higher latency */
  2655. static const int sr_latency_ns = 12000;
  2656. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2657. line_time_us = ((sr_hdisplay * 1000) / sr_clock);
  2658. /* Use ns/us then divide to preserve precision */
  2659. sr_entries = (((sr_latency_ns / line_time_us) + 1) *
  2660. pixel_size * sr_hdisplay) / 1000;
  2661. sr_entries = roundup(sr_entries / cacheline_size, 1);
  2662. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2663. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2664. } else {
  2665. /* Turn off self refresh if both pipes are enabled */
  2666. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2667. & ~FW_BLC_SELF_EN);
  2668. }
  2669. DRM_DEBUG("Setting FIFO watermarks - A: %d, B: %d, SR %d\n",
  2670. planea_wm, planeb_wm, sr_entries);
  2671. planea_wm &= 0x3f;
  2672. planeb_wm &= 0x3f;
  2673. I915_WRITE(DSPFW1, (sr_entries << DSPFW_SR_SHIFT) |
  2674. (cursorb_wm << DSPFW_CURSORB_SHIFT) |
  2675. (planeb_wm << DSPFW_PLANEB_SHIFT) | planea_wm);
  2676. I915_WRITE(DSPFW2, (I915_READ(DSPFW2) & DSPFW_CURSORA_MASK) |
  2677. (cursora_wm << DSPFW_CURSORA_SHIFT));
  2678. /* HPLL off in SR has some issues on G4x... disable it */
  2679. I915_WRITE(DSPFW3, (I915_READ(DSPFW3) & ~DSPFW_HPLL_SR_EN) |
  2680. (cursor_sr << DSPFW_CURSOR_SR_SHIFT));
  2681. }
  2682. static void i965_update_wm(struct drm_device *dev, int planea_clock,
  2683. int planeb_clock, int sr_hdisplay, int pixel_size)
  2684. {
  2685. struct drm_i915_private *dev_priv = dev->dev_private;
  2686. unsigned long line_time_us;
  2687. int sr_clock, sr_entries, srwm = 1;
  2688. /* Calc sr entries for one plane configs */
  2689. if (sr_hdisplay && (!planea_clock || !planeb_clock)) {
  2690. /* self-refresh has much higher latency */
  2691. static const int sr_latency_ns = 12000;
  2692. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2693. line_time_us = ((sr_hdisplay * 1000) / sr_clock);
  2694. /* Use ns/us then divide to preserve precision */
  2695. sr_entries = (((sr_latency_ns / line_time_us) + 1) *
  2696. pixel_size * sr_hdisplay) / 1000;
  2697. sr_entries = roundup(sr_entries / I915_FIFO_LINE_SIZE, 1);
  2698. DRM_DEBUG("self-refresh entries: %d\n", sr_entries);
  2699. srwm = I945_FIFO_SIZE - sr_entries;
  2700. if (srwm < 0)
  2701. srwm = 1;
  2702. srwm &= 0x3f;
  2703. if (IS_I965GM(dev))
  2704. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  2705. } else {
  2706. /* Turn off self refresh if both pipes are enabled */
  2707. if (IS_I965GM(dev))
  2708. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2709. & ~FW_BLC_SELF_EN);
  2710. }
  2711. DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
  2712. srwm);
  2713. /* 965 has limitations... */
  2714. I915_WRITE(DSPFW1, (srwm << DSPFW_SR_SHIFT) | (8 << 16) | (8 << 8) |
  2715. (8 << 0));
  2716. I915_WRITE(DSPFW2, (8 << 8) | (8 << 0));
  2717. }
  2718. static void i9xx_update_wm(struct drm_device *dev, int planea_clock,
  2719. int planeb_clock, int sr_hdisplay, int pixel_size)
  2720. {
  2721. struct drm_i915_private *dev_priv = dev->dev_private;
  2722. uint32_t fwater_lo;
  2723. uint32_t fwater_hi;
  2724. int total_size, cacheline_size, cwm, srwm = 1;
  2725. int planea_wm, planeb_wm;
  2726. struct intel_watermark_params planea_params, planeb_params;
  2727. unsigned long line_time_us;
  2728. int sr_clock, sr_entries = 0;
  2729. /* Create copies of the base settings for each pipe */
  2730. if (IS_I965GM(dev) || IS_I945GM(dev))
  2731. planea_params = planeb_params = i945_wm_info;
  2732. else if (IS_I9XX(dev))
  2733. planea_params = planeb_params = i915_wm_info;
  2734. else
  2735. planea_params = planeb_params = i855_wm_info;
  2736. /* Grab a couple of global values before we overwrite them */
  2737. total_size = planea_params.fifo_size;
  2738. cacheline_size = planea_params.cacheline_size;
  2739. /* Update per-plane FIFO sizes */
  2740. planea_params.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2741. planeb_params.fifo_size = dev_priv->display.get_fifo_size(dev, 1);
  2742. planea_wm = intel_calculate_wm(planea_clock, &planea_params,
  2743. pixel_size, latency_ns);
  2744. planeb_wm = intel_calculate_wm(planeb_clock, &planeb_params,
  2745. pixel_size, latency_ns);
  2746. DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
  2747. /*
  2748. * Overlay gets an aggressive default since video jitter is bad.
  2749. */
  2750. cwm = 2;
  2751. /* Calc sr entries for one plane configs */
  2752. if (HAS_FW_BLC(dev) && sr_hdisplay &&
  2753. (!planea_clock || !planeb_clock)) {
  2754. /* self-refresh has much higher latency */
  2755. static const int sr_latency_ns = 6000;
  2756. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2757. line_time_us = ((sr_hdisplay * 1000) / sr_clock);
  2758. /* Use ns/us then divide to preserve precision */
  2759. sr_entries = (((sr_latency_ns / line_time_us) + 1) *
  2760. pixel_size * sr_hdisplay) / 1000;
  2761. sr_entries = roundup(sr_entries / cacheline_size, 1);
  2762. DRM_DEBUG_KMS("self-refresh entries: %d\n", sr_entries);
  2763. srwm = total_size - sr_entries;
  2764. if (srwm < 0)
  2765. srwm = 1;
  2766. if (IS_I945G(dev) || IS_I945GM(dev))
  2767. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
  2768. else if (IS_I915GM(dev)) {
  2769. /* 915M has a smaller SRWM field */
  2770. I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
  2771. I915_WRITE(INSTPM, I915_READ(INSTPM) | INSTPM_SELF_EN);
  2772. }
  2773. } else {
  2774. /* Turn off self refresh if both pipes are enabled */
  2775. if (IS_I945G(dev) || IS_I945GM(dev)) {
  2776. I915_WRITE(FW_BLC_SELF, I915_READ(FW_BLC_SELF)
  2777. & ~FW_BLC_SELF_EN);
  2778. } else if (IS_I915GM(dev)) {
  2779. I915_WRITE(INSTPM, I915_READ(INSTPM) & ~INSTPM_SELF_EN);
  2780. }
  2781. }
  2782. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
  2783. planea_wm, planeb_wm, cwm, srwm);
  2784. fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
  2785. fwater_hi = (cwm & 0x1f);
  2786. /* Set request length to 8 cachelines per fetch */
  2787. fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
  2788. fwater_hi = fwater_hi | (1 << 8);
  2789. I915_WRITE(FW_BLC, fwater_lo);
  2790. I915_WRITE(FW_BLC2, fwater_hi);
  2791. }
  2792. static void i830_update_wm(struct drm_device *dev, int planea_clock, int unused,
  2793. int unused2, int pixel_size)
  2794. {
  2795. struct drm_i915_private *dev_priv = dev->dev_private;
  2796. uint32_t fwater_lo = I915_READ(FW_BLC) & ~0xfff;
  2797. int planea_wm;
  2798. i830_wm_info.fifo_size = dev_priv->display.get_fifo_size(dev, 0);
  2799. planea_wm = intel_calculate_wm(planea_clock, &i830_wm_info,
  2800. pixel_size, latency_ns);
  2801. fwater_lo |= (3<<8) | planea_wm;
  2802. DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
  2803. I915_WRITE(FW_BLC, fwater_lo);
  2804. }
  2805. #define ILK_LP0_PLANE_LATENCY 700
  2806. static void ironlake_update_wm(struct drm_device *dev, int planea_clock,
  2807. int planeb_clock, int sr_hdisplay, int pixel_size)
  2808. {
  2809. struct drm_i915_private *dev_priv = dev->dev_private;
  2810. int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
  2811. int sr_wm, cursor_wm;
  2812. unsigned long line_time_us;
  2813. int sr_clock, entries_required;
  2814. u32 reg_value;
  2815. /* Calculate and update the watermark for plane A */
  2816. if (planea_clock) {
  2817. entries_required = ((planea_clock / 1000) * pixel_size *
  2818. ILK_LP0_PLANE_LATENCY) / 1000;
  2819. entries_required = DIV_ROUND_UP(entries_required,
  2820. ironlake_display_wm_info.cacheline_size);
  2821. planea_wm = entries_required +
  2822. ironlake_display_wm_info.guard_size;
  2823. if (planea_wm > (int)ironlake_display_wm_info.max_wm)
  2824. planea_wm = ironlake_display_wm_info.max_wm;
  2825. cursora_wm = 16;
  2826. reg_value = I915_READ(WM0_PIPEA_ILK);
  2827. reg_value &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  2828. reg_value |= (planea_wm << WM0_PIPE_PLANE_SHIFT) |
  2829. (cursora_wm & WM0_PIPE_CURSOR_MASK);
  2830. I915_WRITE(WM0_PIPEA_ILK, reg_value);
  2831. DRM_DEBUG_KMS("FIFO watermarks For pipe A - plane %d, "
  2832. "cursor: %d\n", planea_wm, cursora_wm);
  2833. }
  2834. /* Calculate and update the watermark for plane B */
  2835. if (planeb_clock) {
  2836. entries_required = ((planeb_clock / 1000) * pixel_size *
  2837. ILK_LP0_PLANE_LATENCY) / 1000;
  2838. entries_required = DIV_ROUND_UP(entries_required,
  2839. ironlake_display_wm_info.cacheline_size);
  2840. planeb_wm = entries_required +
  2841. ironlake_display_wm_info.guard_size;
  2842. if (planeb_wm > (int)ironlake_display_wm_info.max_wm)
  2843. planeb_wm = ironlake_display_wm_info.max_wm;
  2844. cursorb_wm = 16;
  2845. reg_value = I915_READ(WM0_PIPEB_ILK);
  2846. reg_value &= ~(WM0_PIPE_PLANE_MASK | WM0_PIPE_CURSOR_MASK);
  2847. reg_value |= (planeb_wm << WM0_PIPE_PLANE_SHIFT) |
  2848. (cursorb_wm & WM0_PIPE_CURSOR_MASK);
  2849. I915_WRITE(WM0_PIPEB_ILK, reg_value);
  2850. DRM_DEBUG_KMS("FIFO watermarks For pipe B - plane %d, "
  2851. "cursor: %d\n", planeb_wm, cursorb_wm);
  2852. }
  2853. /*
  2854. * Calculate and update the self-refresh watermark only when one
  2855. * display plane is used.
  2856. */
  2857. if (!planea_clock || !planeb_clock) {
  2858. int line_count;
  2859. /* Read the self-refresh latency. The unit is 0.5us */
  2860. int ilk_sr_latency = I915_READ(MLTR_ILK) & ILK_SRLT_MASK;
  2861. sr_clock = planea_clock ? planea_clock : planeb_clock;
  2862. line_time_us = ((sr_hdisplay * 1000) / sr_clock);
  2863. /* Use ns/us then divide to preserve precision */
  2864. line_count = ((ilk_sr_latency * 500) / line_time_us + 1000)
  2865. / 1000;
  2866. /* calculate the self-refresh watermark for display plane */
  2867. entries_required = line_count * sr_hdisplay * pixel_size;
  2868. entries_required = DIV_ROUND_UP(entries_required,
  2869. ironlake_display_srwm_info.cacheline_size);
  2870. sr_wm = entries_required +
  2871. ironlake_display_srwm_info.guard_size;
  2872. /* calculate the self-refresh watermark for display cursor */
  2873. entries_required = line_count * pixel_size * 64;
  2874. entries_required = DIV_ROUND_UP(entries_required,
  2875. ironlake_cursor_srwm_info.cacheline_size);
  2876. cursor_wm = entries_required +
  2877. ironlake_cursor_srwm_info.guard_size;
  2878. /* configure watermark and enable self-refresh */
  2879. reg_value = I915_READ(WM1_LP_ILK);
  2880. reg_value &= ~(WM1_LP_LATENCY_MASK | WM1_LP_SR_MASK |
  2881. WM1_LP_CURSOR_MASK);
  2882. reg_value |= WM1_LP_SR_EN |
  2883. (ilk_sr_latency << WM1_LP_LATENCY_SHIFT) |
  2884. (sr_wm << WM1_LP_SR_SHIFT) | cursor_wm;
  2885. I915_WRITE(WM1_LP_ILK, reg_value);
  2886. DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
  2887. "cursor %d\n", sr_wm, cursor_wm);
  2888. } else {
  2889. /* Turn off self refresh if both pipes are enabled */
  2890. I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
  2891. }
  2892. }
  2893. /**
  2894. * intel_update_watermarks - update FIFO watermark values based on current modes
  2895. *
  2896. * Calculate watermark values for the various WM regs based on current mode
  2897. * and plane configuration.
  2898. *
  2899. * There are several cases to deal with here:
  2900. * - normal (i.e. non-self-refresh)
  2901. * - self-refresh (SR) mode
  2902. * - lines are large relative to FIFO size (buffer can hold up to 2)
  2903. * - lines are small relative to FIFO size (buffer can hold more than 2
  2904. * lines), so need to account for TLB latency
  2905. *
  2906. * The normal calculation is:
  2907. * watermark = dotclock * bytes per pixel * latency
  2908. * where latency is platform & configuration dependent (we assume pessimal
  2909. * values here).
  2910. *
  2911. * The SR calculation is:
  2912. * watermark = (trunc(latency/line time)+1) * surface width *
  2913. * bytes per pixel
  2914. * where
  2915. * line time = htotal / dotclock
  2916. * and latency is assumed to be high, as above.
  2917. *
  2918. * The final value programmed to the register should always be rounded up,
  2919. * and include an extra 2 entries to account for clock crossings.
  2920. *
  2921. * We don't use the sprite, so we can ignore that. And on Crestline we have
  2922. * to set the non-SR watermarks to 8.
  2923. */
  2924. static void intel_update_watermarks(struct drm_device *dev)
  2925. {
  2926. struct drm_i915_private *dev_priv = dev->dev_private;
  2927. struct drm_crtc *crtc;
  2928. struct intel_crtc *intel_crtc;
  2929. int sr_hdisplay = 0;
  2930. unsigned long planea_clock = 0, planeb_clock = 0, sr_clock = 0;
  2931. int enabled = 0, pixel_size = 0;
  2932. if (!dev_priv->display.update_wm)
  2933. return;
  2934. /* Get the clock config from both planes */
  2935. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  2936. intel_crtc = to_intel_crtc(crtc);
  2937. if (crtc->enabled) {
  2938. enabled++;
  2939. if (intel_crtc->plane == 0) {
  2940. DRM_DEBUG_KMS("plane A (pipe %d) clock: %d\n",
  2941. intel_crtc->pipe, crtc->mode.clock);
  2942. planea_clock = crtc->mode.clock;
  2943. } else {
  2944. DRM_DEBUG_KMS("plane B (pipe %d) clock: %d\n",
  2945. intel_crtc->pipe, crtc->mode.clock);
  2946. planeb_clock = crtc->mode.clock;
  2947. }
  2948. sr_hdisplay = crtc->mode.hdisplay;
  2949. sr_clock = crtc->mode.clock;
  2950. if (crtc->fb)
  2951. pixel_size = crtc->fb->bits_per_pixel / 8;
  2952. else
  2953. pixel_size = 4; /* by default */
  2954. }
  2955. }
  2956. if (enabled <= 0)
  2957. return;
  2958. dev_priv->display.update_wm(dev, planea_clock, planeb_clock,
  2959. sr_hdisplay, pixel_size);
  2960. }
  2961. static int intel_crtc_mode_set(struct drm_crtc *crtc,
  2962. struct drm_display_mode *mode,
  2963. struct drm_display_mode *adjusted_mode,
  2964. int x, int y,
  2965. struct drm_framebuffer *old_fb)
  2966. {
  2967. struct drm_device *dev = crtc->dev;
  2968. struct drm_i915_private *dev_priv = dev->dev_private;
  2969. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2970. int pipe = intel_crtc->pipe;
  2971. int plane = intel_crtc->plane;
  2972. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  2973. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  2974. int dpll_md_reg = (intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  2975. int dspcntr_reg = (plane == 0) ? DSPACNTR : DSPBCNTR;
  2976. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  2977. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  2978. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  2979. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  2980. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  2981. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  2982. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  2983. int dspsize_reg = (plane == 0) ? DSPASIZE : DSPBSIZE;
  2984. int dsppos_reg = (plane == 0) ? DSPAPOS : DSPBPOS;
  2985. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  2986. int refclk, num_connectors = 0;
  2987. intel_clock_t clock, reduced_clock;
  2988. u32 dpll = 0, fp = 0, fp2 = 0, dspcntr, pipeconf;
  2989. bool ok, has_reduced_clock = false, is_sdvo = false, is_dvo = false;
  2990. bool is_crt = false, is_lvds = false, is_tv = false, is_dp = false;
  2991. bool is_edp = false;
  2992. struct drm_mode_config *mode_config = &dev->mode_config;
  2993. struct drm_encoder *encoder;
  2994. struct intel_encoder *intel_encoder = NULL;
  2995. const intel_limit_t *limit;
  2996. int ret;
  2997. struct fdi_m_n m_n = {0};
  2998. int data_m1_reg = (pipe == 0) ? PIPEA_DATA_M1 : PIPEB_DATA_M1;
  2999. int data_n1_reg = (pipe == 0) ? PIPEA_DATA_N1 : PIPEB_DATA_N1;
  3000. int link_m1_reg = (pipe == 0) ? PIPEA_LINK_M1 : PIPEB_LINK_M1;
  3001. int link_n1_reg = (pipe == 0) ? PIPEA_LINK_N1 : PIPEB_LINK_N1;
  3002. int pch_fp_reg = (pipe == 0) ? PCH_FPA0 : PCH_FPB0;
  3003. int pch_dpll_reg = (pipe == 0) ? PCH_DPLL_A : PCH_DPLL_B;
  3004. int fdi_rx_reg = (pipe == 0) ? FDI_RXA_CTL : FDI_RXB_CTL;
  3005. int fdi_tx_reg = (pipe == 0) ? FDI_TXA_CTL : FDI_TXB_CTL;
  3006. int trans_dpll_sel = (pipe == 0) ? 0 : 1;
  3007. int lvds_reg = LVDS;
  3008. u32 temp;
  3009. int sdvo_pixel_multiply;
  3010. int target_clock;
  3011. drm_vblank_pre_modeset(dev, pipe);
  3012. list_for_each_entry(encoder, &mode_config->encoder_list, head) {
  3013. if (!encoder || encoder->crtc != crtc)
  3014. continue;
  3015. intel_encoder = enc_to_intel_encoder(encoder);
  3016. switch (intel_encoder->type) {
  3017. case INTEL_OUTPUT_LVDS:
  3018. is_lvds = true;
  3019. break;
  3020. case INTEL_OUTPUT_SDVO:
  3021. case INTEL_OUTPUT_HDMI:
  3022. is_sdvo = true;
  3023. if (intel_encoder->needs_tv_clock)
  3024. is_tv = true;
  3025. break;
  3026. case INTEL_OUTPUT_DVO:
  3027. is_dvo = true;
  3028. break;
  3029. case INTEL_OUTPUT_TVOUT:
  3030. is_tv = true;
  3031. break;
  3032. case INTEL_OUTPUT_ANALOG:
  3033. is_crt = true;
  3034. break;
  3035. case INTEL_OUTPUT_DISPLAYPORT:
  3036. is_dp = true;
  3037. break;
  3038. case INTEL_OUTPUT_EDP:
  3039. is_edp = true;
  3040. break;
  3041. }
  3042. num_connectors++;
  3043. }
  3044. if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2) {
  3045. refclk = dev_priv->lvds_ssc_freq * 1000;
  3046. DRM_DEBUG_KMS("using SSC reference clock of %d MHz\n",
  3047. refclk / 1000);
  3048. } else if (IS_I9XX(dev)) {
  3049. refclk = 96000;
  3050. if (HAS_PCH_SPLIT(dev))
  3051. refclk = 120000; /* 120Mhz refclk */
  3052. } else {
  3053. refclk = 48000;
  3054. }
  3055. /*
  3056. * Returns a set of divisors for the desired target clock with the given
  3057. * refclk, or FALSE. The returned values represent the clock equation:
  3058. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  3059. */
  3060. limit = intel_limit(crtc);
  3061. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk, &clock);
  3062. if (!ok) {
  3063. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  3064. drm_vblank_post_modeset(dev, pipe);
  3065. return -EINVAL;
  3066. }
  3067. if (is_lvds && dev_priv->lvds_downclock_avail) {
  3068. has_reduced_clock = limit->find_pll(limit, crtc,
  3069. dev_priv->lvds_downclock,
  3070. refclk,
  3071. &reduced_clock);
  3072. if (has_reduced_clock && (clock.p != reduced_clock.p)) {
  3073. /*
  3074. * If the different P is found, it means that we can't
  3075. * switch the display clock by using the FP0/FP1.
  3076. * In such case we will disable the LVDS downclock
  3077. * feature.
  3078. */
  3079. DRM_DEBUG_KMS("Different P is found for "
  3080. "LVDS clock/downclock\n");
  3081. has_reduced_clock = 0;
  3082. }
  3083. }
  3084. /* SDVO TV has fixed PLL values depend on its clock range,
  3085. this mirrors vbios setting. */
  3086. if (is_sdvo && is_tv) {
  3087. if (adjusted_mode->clock >= 100000
  3088. && adjusted_mode->clock < 140500) {
  3089. clock.p1 = 2;
  3090. clock.p2 = 10;
  3091. clock.n = 3;
  3092. clock.m1 = 16;
  3093. clock.m2 = 8;
  3094. } else if (adjusted_mode->clock >= 140500
  3095. && adjusted_mode->clock <= 200000) {
  3096. clock.p1 = 1;
  3097. clock.p2 = 10;
  3098. clock.n = 6;
  3099. clock.m1 = 12;
  3100. clock.m2 = 8;
  3101. }
  3102. }
  3103. /* FDI link */
  3104. if (HAS_PCH_SPLIT(dev)) {
  3105. int lane = 0, link_bw, bpp;
  3106. /* eDP doesn't require FDI link, so just set DP M/N
  3107. according to current link config */
  3108. if (is_edp) {
  3109. target_clock = mode->clock;
  3110. intel_edp_link_config(intel_encoder,
  3111. &lane, &link_bw);
  3112. } else {
  3113. /* DP over FDI requires target mode clock
  3114. instead of link clock */
  3115. if (is_dp)
  3116. target_clock = mode->clock;
  3117. else
  3118. target_clock = adjusted_mode->clock;
  3119. link_bw = 270000;
  3120. }
  3121. /* determine panel color depth */
  3122. temp = I915_READ(pipeconf_reg);
  3123. temp &= ~PIPE_BPC_MASK;
  3124. if (is_lvds) {
  3125. int lvds_reg = I915_READ(PCH_LVDS);
  3126. /* the BPC will be 6 if it is 18-bit LVDS panel */
  3127. if ((lvds_reg & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  3128. temp |= PIPE_8BPC;
  3129. else
  3130. temp |= PIPE_6BPC;
  3131. } else if (is_edp) {
  3132. switch (dev_priv->edp_bpp/3) {
  3133. case 8:
  3134. temp |= PIPE_8BPC;
  3135. break;
  3136. case 10:
  3137. temp |= PIPE_10BPC;
  3138. break;
  3139. case 6:
  3140. temp |= PIPE_6BPC;
  3141. break;
  3142. case 12:
  3143. temp |= PIPE_12BPC;
  3144. break;
  3145. }
  3146. } else
  3147. temp |= PIPE_8BPC;
  3148. I915_WRITE(pipeconf_reg, temp);
  3149. I915_READ(pipeconf_reg);
  3150. switch (temp & PIPE_BPC_MASK) {
  3151. case PIPE_8BPC:
  3152. bpp = 24;
  3153. break;
  3154. case PIPE_10BPC:
  3155. bpp = 30;
  3156. break;
  3157. case PIPE_6BPC:
  3158. bpp = 18;
  3159. break;
  3160. case PIPE_12BPC:
  3161. bpp = 36;
  3162. break;
  3163. default:
  3164. DRM_ERROR("unknown pipe bpc value\n");
  3165. bpp = 24;
  3166. }
  3167. if (!lane) {
  3168. /*
  3169. * Account for spread spectrum to avoid
  3170. * oversubscribing the link. Max center spread
  3171. * is 2.5%; use 5% for safety's sake.
  3172. */
  3173. u32 bps = target_clock * bpp * 21 / 20;
  3174. lane = bps / (link_bw * 8) + 1;
  3175. }
  3176. intel_crtc->fdi_lanes = lane;
  3177. ironlake_compute_m_n(bpp, lane, target_clock, link_bw, &m_n);
  3178. }
  3179. /* Ironlake: try to setup display ref clock before DPLL
  3180. * enabling. This is only under driver's control after
  3181. * PCH B stepping, previous chipset stepping should be
  3182. * ignoring this setting.
  3183. */
  3184. if (HAS_PCH_SPLIT(dev)) {
  3185. temp = I915_READ(PCH_DREF_CONTROL);
  3186. /* Always enable nonspread source */
  3187. temp &= ~DREF_NONSPREAD_SOURCE_MASK;
  3188. temp |= DREF_NONSPREAD_SOURCE_ENABLE;
  3189. I915_WRITE(PCH_DREF_CONTROL, temp);
  3190. POSTING_READ(PCH_DREF_CONTROL);
  3191. temp &= ~DREF_SSC_SOURCE_MASK;
  3192. temp |= DREF_SSC_SOURCE_ENABLE;
  3193. I915_WRITE(PCH_DREF_CONTROL, temp);
  3194. POSTING_READ(PCH_DREF_CONTROL);
  3195. udelay(200);
  3196. if (is_edp) {
  3197. if (dev_priv->lvds_use_ssc) {
  3198. temp |= DREF_SSC1_ENABLE;
  3199. I915_WRITE(PCH_DREF_CONTROL, temp);
  3200. POSTING_READ(PCH_DREF_CONTROL);
  3201. udelay(200);
  3202. temp &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  3203. temp |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  3204. I915_WRITE(PCH_DREF_CONTROL, temp);
  3205. POSTING_READ(PCH_DREF_CONTROL);
  3206. } else {
  3207. temp |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  3208. I915_WRITE(PCH_DREF_CONTROL, temp);
  3209. POSTING_READ(PCH_DREF_CONTROL);
  3210. }
  3211. }
  3212. }
  3213. if (IS_PINEVIEW(dev)) {
  3214. fp = (1 << clock.n) << 16 | clock.m1 << 8 | clock.m2;
  3215. if (has_reduced_clock)
  3216. fp2 = (1 << reduced_clock.n) << 16 |
  3217. reduced_clock.m1 << 8 | reduced_clock.m2;
  3218. } else {
  3219. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  3220. if (has_reduced_clock)
  3221. fp2 = reduced_clock.n << 16 | reduced_clock.m1 << 8 |
  3222. reduced_clock.m2;
  3223. }
  3224. if (!HAS_PCH_SPLIT(dev))
  3225. dpll = DPLL_VGA_MODE_DIS;
  3226. if (IS_I9XX(dev)) {
  3227. if (is_lvds)
  3228. dpll |= DPLLB_MODE_LVDS;
  3229. else
  3230. dpll |= DPLLB_MODE_DAC_SERIAL;
  3231. if (is_sdvo) {
  3232. dpll |= DPLL_DVO_HIGH_SPEED;
  3233. sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  3234. if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3235. dpll |= (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  3236. else if (HAS_PCH_SPLIT(dev))
  3237. dpll |= (sdvo_pixel_multiply - 1) << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  3238. }
  3239. if (is_dp)
  3240. dpll |= DPLL_DVO_HIGH_SPEED;
  3241. /* compute bitmask from p1 value */
  3242. if (IS_PINEVIEW(dev))
  3243. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  3244. else {
  3245. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3246. /* also FPA1 */
  3247. if (HAS_PCH_SPLIT(dev))
  3248. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3249. if (IS_G4X(dev) && has_reduced_clock)
  3250. dpll |= (1 << (reduced_clock.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  3251. }
  3252. switch (clock.p2) {
  3253. case 5:
  3254. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  3255. break;
  3256. case 7:
  3257. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  3258. break;
  3259. case 10:
  3260. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  3261. break;
  3262. case 14:
  3263. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  3264. break;
  3265. }
  3266. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev))
  3267. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  3268. } else {
  3269. if (is_lvds) {
  3270. dpll |= (1 << (clock.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3271. } else {
  3272. if (clock.p1 == 2)
  3273. dpll |= PLL_P1_DIVIDE_BY_TWO;
  3274. else
  3275. dpll |= (clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  3276. if (clock.p2 == 4)
  3277. dpll |= PLL_P2_DIVIDE_BY_4;
  3278. }
  3279. }
  3280. if (is_sdvo && is_tv)
  3281. dpll |= PLL_REF_INPUT_TVCLKINBC;
  3282. else if (is_tv)
  3283. /* XXX: just matching BIOS for now */
  3284. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  3285. dpll |= 3;
  3286. else if (is_lvds && dev_priv->lvds_use_ssc && num_connectors < 2)
  3287. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  3288. else
  3289. dpll |= PLL_REF_INPUT_DREFCLK;
  3290. /* setup pipeconf */
  3291. pipeconf = I915_READ(pipeconf_reg);
  3292. /* Set up the display plane register */
  3293. dspcntr = DISPPLANE_GAMMA_ENABLE;
  3294. /* Ironlake's plane is forced to pipe, bit 24 is to
  3295. enable color space conversion */
  3296. if (!HAS_PCH_SPLIT(dev)) {
  3297. if (pipe == 0)
  3298. dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
  3299. else
  3300. dspcntr |= DISPPLANE_SEL_PIPE_B;
  3301. }
  3302. if (pipe == 0 && !IS_I965G(dev)) {
  3303. /* Enable pixel doubling when the dot clock is > 90% of the (display)
  3304. * core speed.
  3305. *
  3306. * XXX: No double-wide on 915GM pipe B. Is that the only reason for the
  3307. * pipe == 0 check?
  3308. */
  3309. if (mode->clock >
  3310. dev_priv->display.get_display_clock_speed(dev) * 9 / 10)
  3311. pipeconf |= PIPEACONF_DOUBLE_WIDE;
  3312. else
  3313. pipeconf &= ~PIPEACONF_DOUBLE_WIDE;
  3314. }
  3315. dspcntr |= DISPLAY_PLANE_ENABLE;
  3316. pipeconf |= PIPEACONF_ENABLE;
  3317. dpll |= DPLL_VCO_ENABLE;
  3318. /* Disable the panel fitter if it was on our pipe */
  3319. if (!HAS_PCH_SPLIT(dev) && intel_panel_fitter_pipe(dev) == pipe)
  3320. I915_WRITE(PFIT_CONTROL, 0);
  3321. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  3322. drm_mode_debug_printmodeline(mode);
  3323. /* assign to Ironlake registers */
  3324. if (HAS_PCH_SPLIT(dev)) {
  3325. fp_reg = pch_fp_reg;
  3326. dpll_reg = pch_dpll_reg;
  3327. }
  3328. if (is_edp) {
  3329. ironlake_disable_pll_edp(crtc);
  3330. } else if ((dpll & DPLL_VCO_ENABLE)) {
  3331. I915_WRITE(fp_reg, fp);
  3332. I915_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  3333. I915_READ(dpll_reg);
  3334. udelay(150);
  3335. }
  3336. /* enable transcoder DPLL */
  3337. if (HAS_PCH_CPT(dev)) {
  3338. temp = I915_READ(PCH_DPLL_SEL);
  3339. if (trans_dpll_sel == 0)
  3340. temp |= (TRANSA_DPLL_ENABLE | TRANSA_DPLLA_SEL);
  3341. else
  3342. temp |= (TRANSB_DPLL_ENABLE | TRANSB_DPLLB_SEL);
  3343. I915_WRITE(PCH_DPLL_SEL, temp);
  3344. I915_READ(PCH_DPLL_SEL);
  3345. udelay(150);
  3346. }
  3347. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  3348. * This is an exception to the general rule that mode_set doesn't turn
  3349. * things on.
  3350. */
  3351. if (is_lvds) {
  3352. u32 lvds;
  3353. if (HAS_PCH_SPLIT(dev))
  3354. lvds_reg = PCH_LVDS;
  3355. lvds = I915_READ(lvds_reg);
  3356. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  3357. if (pipe == 1) {
  3358. if (HAS_PCH_CPT(dev))
  3359. lvds |= PORT_TRANS_B_SEL_CPT;
  3360. else
  3361. lvds |= LVDS_PIPEB_SELECT;
  3362. } else {
  3363. if (HAS_PCH_CPT(dev))
  3364. lvds &= ~PORT_TRANS_SEL_MASK;
  3365. else
  3366. lvds &= ~LVDS_PIPEB_SELECT;
  3367. }
  3368. /* set the corresponsding LVDS_BORDER bit */
  3369. lvds |= dev_priv->lvds_border_bits;
  3370. /* Set the B0-B3 data pairs corresponding to whether we're going to
  3371. * set the DPLLs for dual-channel mode or not.
  3372. */
  3373. if (clock.p2 == 7)
  3374. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  3375. else
  3376. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  3377. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  3378. * appropriately here, but we need to look more thoroughly into how
  3379. * panels behave in the two modes.
  3380. */
  3381. /* set the dithering flag */
  3382. if (IS_I965G(dev)) {
  3383. if (dev_priv->lvds_dither) {
  3384. if (HAS_PCH_SPLIT(dev)) {
  3385. pipeconf |= PIPE_ENABLE_DITHER;
  3386. pipeconf &= ~PIPE_DITHER_TYPE_MASK;
  3387. pipeconf |= PIPE_DITHER_TYPE_ST01;
  3388. } else
  3389. lvds |= LVDS_ENABLE_DITHER;
  3390. } else {
  3391. if (HAS_PCH_SPLIT(dev)) {
  3392. pipeconf &= ~PIPE_ENABLE_DITHER;
  3393. pipeconf &= ~PIPE_DITHER_TYPE_MASK;
  3394. } else
  3395. lvds &= ~LVDS_ENABLE_DITHER;
  3396. }
  3397. }
  3398. I915_WRITE(lvds_reg, lvds);
  3399. I915_READ(lvds_reg);
  3400. }
  3401. if (is_dp)
  3402. intel_dp_set_m_n(crtc, mode, adjusted_mode);
  3403. else if (HAS_PCH_SPLIT(dev)) {
  3404. /* For non-DP output, clear any trans DP clock recovery setting.*/
  3405. if (pipe == 0) {
  3406. I915_WRITE(TRANSA_DATA_M1, 0);
  3407. I915_WRITE(TRANSA_DATA_N1, 0);
  3408. I915_WRITE(TRANSA_DP_LINK_M1, 0);
  3409. I915_WRITE(TRANSA_DP_LINK_N1, 0);
  3410. } else {
  3411. I915_WRITE(TRANSB_DATA_M1, 0);
  3412. I915_WRITE(TRANSB_DATA_N1, 0);
  3413. I915_WRITE(TRANSB_DP_LINK_M1, 0);
  3414. I915_WRITE(TRANSB_DP_LINK_N1, 0);
  3415. }
  3416. }
  3417. if (!is_edp) {
  3418. I915_WRITE(fp_reg, fp);
  3419. I915_WRITE(dpll_reg, dpll);
  3420. I915_READ(dpll_reg);
  3421. /* Wait for the clocks to stabilize. */
  3422. udelay(150);
  3423. if (IS_I965G(dev) && !HAS_PCH_SPLIT(dev)) {
  3424. if (is_sdvo) {
  3425. sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  3426. I915_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) |
  3427. ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  3428. } else
  3429. I915_WRITE(dpll_md_reg, 0);
  3430. } else {
  3431. /* write it again -- the BIOS does, after all */
  3432. I915_WRITE(dpll_reg, dpll);
  3433. }
  3434. I915_READ(dpll_reg);
  3435. /* Wait for the clocks to stabilize. */
  3436. udelay(150);
  3437. }
  3438. if (is_lvds && has_reduced_clock && i915_powersave) {
  3439. I915_WRITE(fp_reg + 4, fp2);
  3440. intel_crtc->lowfreq_avail = true;
  3441. if (HAS_PIPE_CXSR(dev)) {
  3442. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  3443. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  3444. }
  3445. } else {
  3446. I915_WRITE(fp_reg + 4, fp);
  3447. intel_crtc->lowfreq_avail = false;
  3448. if (HAS_PIPE_CXSR(dev)) {
  3449. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  3450. pipeconf &= ~PIPECONF_CXSR_DOWNCLOCK;
  3451. }
  3452. }
  3453. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  3454. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  3455. /* the chip adds 2 halflines automatically */
  3456. adjusted_mode->crtc_vdisplay -= 1;
  3457. adjusted_mode->crtc_vtotal -= 1;
  3458. adjusted_mode->crtc_vblank_start -= 1;
  3459. adjusted_mode->crtc_vblank_end -= 1;
  3460. adjusted_mode->crtc_vsync_end -= 1;
  3461. adjusted_mode->crtc_vsync_start -= 1;
  3462. } else
  3463. pipeconf &= ~PIPECONF_INTERLACE_W_FIELD_INDICATION; /* progressive */
  3464. I915_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  3465. ((adjusted_mode->crtc_htotal - 1) << 16));
  3466. I915_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  3467. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  3468. I915_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  3469. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  3470. I915_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  3471. ((adjusted_mode->crtc_vtotal - 1) << 16));
  3472. I915_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  3473. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  3474. I915_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  3475. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  3476. /* pipesrc and dspsize control the size that is scaled from, which should
  3477. * always be the user's requested size.
  3478. */
  3479. if (!HAS_PCH_SPLIT(dev)) {
  3480. I915_WRITE(dspsize_reg, ((mode->vdisplay - 1) << 16) |
  3481. (mode->hdisplay - 1));
  3482. I915_WRITE(dsppos_reg, 0);
  3483. }
  3484. I915_WRITE(pipesrc_reg, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  3485. if (HAS_PCH_SPLIT(dev)) {
  3486. I915_WRITE(data_m1_reg, TU_SIZE(m_n.tu) | m_n.gmch_m);
  3487. I915_WRITE(data_n1_reg, TU_SIZE(m_n.tu) | m_n.gmch_n);
  3488. I915_WRITE(link_m1_reg, m_n.link_m);
  3489. I915_WRITE(link_n1_reg, m_n.link_n);
  3490. if (is_edp) {
  3491. ironlake_set_pll_edp(crtc, adjusted_mode->clock);
  3492. } else {
  3493. /* enable FDI RX PLL too */
  3494. temp = I915_READ(fdi_rx_reg);
  3495. I915_WRITE(fdi_rx_reg, temp | FDI_RX_PLL_ENABLE);
  3496. I915_READ(fdi_rx_reg);
  3497. udelay(200);
  3498. /* enable FDI TX PLL too */
  3499. temp = I915_READ(fdi_tx_reg);
  3500. I915_WRITE(fdi_tx_reg, temp | FDI_TX_PLL_ENABLE);
  3501. I915_READ(fdi_tx_reg);
  3502. /* enable FDI RX PCDCLK */
  3503. temp = I915_READ(fdi_rx_reg);
  3504. I915_WRITE(fdi_rx_reg, temp | FDI_SEL_PCDCLK);
  3505. I915_READ(fdi_rx_reg);
  3506. udelay(200);
  3507. }
  3508. }
  3509. I915_WRITE(pipeconf_reg, pipeconf);
  3510. I915_READ(pipeconf_reg);
  3511. intel_wait_for_vblank(dev);
  3512. if (IS_IRONLAKE(dev)) {
  3513. /* enable address swizzle for tiling buffer */
  3514. temp = I915_READ(DISP_ARB_CTL);
  3515. I915_WRITE(DISP_ARB_CTL, temp | DISP_TILE_SURFACE_SWIZZLING);
  3516. }
  3517. I915_WRITE(dspcntr_reg, dspcntr);
  3518. /* Flush the plane changes */
  3519. ret = intel_pipe_set_base(crtc, x, y, old_fb);
  3520. if ((IS_I965G(dev) || plane == 0))
  3521. intel_update_fbc(crtc, &crtc->mode);
  3522. intel_update_watermarks(dev);
  3523. drm_vblank_post_modeset(dev, pipe);
  3524. return ret;
  3525. }
  3526. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  3527. void intel_crtc_load_lut(struct drm_crtc *crtc)
  3528. {
  3529. struct drm_device *dev = crtc->dev;
  3530. struct drm_i915_private *dev_priv = dev->dev_private;
  3531. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3532. int palreg = (intel_crtc->pipe == 0) ? PALETTE_A : PALETTE_B;
  3533. int i;
  3534. /* The clocks have to be on to load the palette. */
  3535. if (!crtc->enabled)
  3536. return;
  3537. /* use legacy palette for Ironlake */
  3538. if (HAS_PCH_SPLIT(dev))
  3539. palreg = (intel_crtc->pipe == 0) ? LGC_PALETTE_A :
  3540. LGC_PALETTE_B;
  3541. for (i = 0; i < 256; i++) {
  3542. I915_WRITE(palreg + 4 * i,
  3543. (intel_crtc->lut_r[i] << 16) |
  3544. (intel_crtc->lut_g[i] << 8) |
  3545. intel_crtc->lut_b[i]);
  3546. }
  3547. }
  3548. static int intel_crtc_cursor_set(struct drm_crtc *crtc,
  3549. struct drm_file *file_priv,
  3550. uint32_t handle,
  3551. uint32_t width, uint32_t height)
  3552. {
  3553. struct drm_device *dev = crtc->dev;
  3554. struct drm_i915_private *dev_priv = dev->dev_private;
  3555. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3556. struct drm_gem_object *bo;
  3557. struct drm_i915_gem_object *obj_priv;
  3558. int pipe = intel_crtc->pipe;
  3559. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  3560. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  3561. uint32_t temp = I915_READ(control);
  3562. size_t addr;
  3563. int ret;
  3564. DRM_DEBUG_KMS("\n");
  3565. /* if we want to turn off the cursor ignore width and height */
  3566. if (!handle) {
  3567. DRM_DEBUG_KMS("cursor off\n");
  3568. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  3569. temp &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
  3570. temp |= CURSOR_MODE_DISABLE;
  3571. } else {
  3572. temp &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
  3573. }
  3574. addr = 0;
  3575. bo = NULL;
  3576. mutex_lock(&dev->struct_mutex);
  3577. goto finish;
  3578. }
  3579. /* Currently we only support 64x64 cursors */
  3580. if (width != 64 || height != 64) {
  3581. DRM_ERROR("we currently only support 64x64 cursors\n");
  3582. return -EINVAL;
  3583. }
  3584. bo = drm_gem_object_lookup(dev, file_priv, handle);
  3585. if (!bo)
  3586. return -ENOENT;
  3587. obj_priv = to_intel_bo(bo);
  3588. if (bo->size < width * height * 4) {
  3589. DRM_ERROR("buffer is to small\n");
  3590. ret = -ENOMEM;
  3591. goto fail;
  3592. }
  3593. /* we only need to pin inside GTT if cursor is non-phy */
  3594. mutex_lock(&dev->struct_mutex);
  3595. if (!dev_priv->info->cursor_needs_physical) {
  3596. ret = i915_gem_object_pin(bo, PAGE_SIZE);
  3597. if (ret) {
  3598. DRM_ERROR("failed to pin cursor bo\n");
  3599. goto fail_locked;
  3600. }
  3601. ret = i915_gem_object_set_to_gtt_domain(bo, 0);
  3602. if (ret) {
  3603. DRM_ERROR("failed to move cursor bo into the GTT\n");
  3604. goto fail_unpin;
  3605. }
  3606. addr = obj_priv->gtt_offset;
  3607. } else {
  3608. ret = i915_gem_attach_phys_object(dev, bo, (pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1);
  3609. if (ret) {
  3610. DRM_ERROR("failed to attach phys object\n");
  3611. goto fail_locked;
  3612. }
  3613. addr = obj_priv->phys_obj->handle->busaddr;
  3614. }
  3615. if (!IS_I9XX(dev))
  3616. I915_WRITE(CURSIZE, (height << 12) | width);
  3617. /* Hooray for CUR*CNTR differences */
  3618. if (IS_MOBILE(dev) || IS_I9XX(dev)) {
  3619. temp &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
  3620. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  3621. temp |= (pipe << 28); /* Connect to correct pipe */
  3622. } else {
  3623. temp &= ~(CURSOR_FORMAT_MASK);
  3624. temp |= CURSOR_ENABLE;
  3625. temp |= CURSOR_FORMAT_ARGB | CURSOR_GAMMA_ENABLE;
  3626. }
  3627. finish:
  3628. I915_WRITE(control, temp);
  3629. I915_WRITE(base, addr);
  3630. if (intel_crtc->cursor_bo) {
  3631. if (dev_priv->info->cursor_needs_physical) {
  3632. if (intel_crtc->cursor_bo != bo)
  3633. i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
  3634. } else
  3635. i915_gem_object_unpin(intel_crtc->cursor_bo);
  3636. drm_gem_object_unreference(intel_crtc->cursor_bo);
  3637. }
  3638. mutex_unlock(&dev->struct_mutex);
  3639. intel_crtc->cursor_addr = addr;
  3640. intel_crtc->cursor_bo = bo;
  3641. return 0;
  3642. fail_unpin:
  3643. i915_gem_object_unpin(bo);
  3644. fail_locked:
  3645. mutex_unlock(&dev->struct_mutex);
  3646. fail:
  3647. drm_gem_object_unreference_unlocked(bo);
  3648. return ret;
  3649. }
  3650. static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  3651. {
  3652. struct drm_device *dev = crtc->dev;
  3653. struct drm_i915_private *dev_priv = dev->dev_private;
  3654. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3655. struct intel_framebuffer *intel_fb;
  3656. int pipe = intel_crtc->pipe;
  3657. uint32_t temp = 0;
  3658. uint32_t adder;
  3659. if (crtc->fb) {
  3660. intel_fb = to_intel_framebuffer(crtc->fb);
  3661. intel_mark_busy(dev, intel_fb->obj);
  3662. }
  3663. if (x < 0) {
  3664. temp |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  3665. x = -x;
  3666. }
  3667. if (y < 0) {
  3668. temp |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  3669. y = -y;
  3670. }
  3671. temp |= x << CURSOR_X_SHIFT;
  3672. temp |= y << CURSOR_Y_SHIFT;
  3673. adder = intel_crtc->cursor_addr;
  3674. I915_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  3675. I915_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  3676. return 0;
  3677. }
  3678. /** Sets the color ramps on behalf of RandR */
  3679. void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  3680. u16 blue, int regno)
  3681. {
  3682. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3683. intel_crtc->lut_r[regno] = red >> 8;
  3684. intel_crtc->lut_g[regno] = green >> 8;
  3685. intel_crtc->lut_b[regno] = blue >> 8;
  3686. }
  3687. void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  3688. u16 *blue, int regno)
  3689. {
  3690. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3691. *red = intel_crtc->lut_r[regno] << 8;
  3692. *green = intel_crtc->lut_g[regno] << 8;
  3693. *blue = intel_crtc->lut_b[regno] << 8;
  3694. }
  3695. static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  3696. u16 *blue, uint32_t size)
  3697. {
  3698. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3699. int i;
  3700. if (size != 256)
  3701. return;
  3702. for (i = 0; i < 256; i++) {
  3703. intel_crtc->lut_r[i] = red[i] >> 8;
  3704. intel_crtc->lut_g[i] = green[i] >> 8;
  3705. intel_crtc->lut_b[i] = blue[i] >> 8;
  3706. }
  3707. intel_crtc_load_lut(crtc);
  3708. }
  3709. /**
  3710. * Get a pipe with a simple mode set on it for doing load-based monitor
  3711. * detection.
  3712. *
  3713. * It will be up to the load-detect code to adjust the pipe as appropriate for
  3714. * its requirements. The pipe will be connected to no other encoders.
  3715. *
  3716. * Currently this code will only succeed if there is a pipe with no encoders
  3717. * configured for it. In the future, it could choose to temporarily disable
  3718. * some outputs to free up a pipe for its use.
  3719. *
  3720. * \return crtc, or NULL if no pipes are available.
  3721. */
  3722. /* VESA 640x480x72Hz mode to set on the pipe */
  3723. static struct drm_display_mode load_detect_mode = {
  3724. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  3725. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  3726. };
  3727. struct drm_crtc *intel_get_load_detect_pipe(struct intel_encoder *intel_encoder,
  3728. struct drm_connector *connector,
  3729. struct drm_display_mode *mode,
  3730. int *dpms_mode)
  3731. {
  3732. struct intel_crtc *intel_crtc;
  3733. struct drm_crtc *possible_crtc;
  3734. struct drm_crtc *supported_crtc =NULL;
  3735. struct drm_encoder *encoder = &intel_encoder->enc;
  3736. struct drm_crtc *crtc = NULL;
  3737. struct drm_device *dev = encoder->dev;
  3738. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3739. struct drm_crtc_helper_funcs *crtc_funcs;
  3740. int i = -1;
  3741. /*
  3742. * Algorithm gets a little messy:
  3743. * - if the connector already has an assigned crtc, use it (but make
  3744. * sure it's on first)
  3745. * - try to find the first unused crtc that can drive this connector,
  3746. * and use that if we find one
  3747. * - if there are no unused crtcs available, try to use the first
  3748. * one we found that supports the connector
  3749. */
  3750. /* See if we already have a CRTC for this connector */
  3751. if (encoder->crtc) {
  3752. crtc = encoder->crtc;
  3753. /* Make sure the crtc and connector are running */
  3754. intel_crtc = to_intel_crtc(crtc);
  3755. *dpms_mode = intel_crtc->dpms_mode;
  3756. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3757. crtc_funcs = crtc->helper_private;
  3758. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3759. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  3760. }
  3761. return crtc;
  3762. }
  3763. /* Find an unused one (if possible) */
  3764. list_for_each_entry(possible_crtc, &dev->mode_config.crtc_list, head) {
  3765. i++;
  3766. if (!(encoder->possible_crtcs & (1 << i)))
  3767. continue;
  3768. if (!possible_crtc->enabled) {
  3769. crtc = possible_crtc;
  3770. break;
  3771. }
  3772. if (!supported_crtc)
  3773. supported_crtc = possible_crtc;
  3774. }
  3775. /*
  3776. * If we didn't find an unused CRTC, don't use any.
  3777. */
  3778. if (!crtc) {
  3779. return NULL;
  3780. }
  3781. encoder->crtc = crtc;
  3782. connector->encoder = encoder;
  3783. intel_encoder->load_detect_temp = true;
  3784. intel_crtc = to_intel_crtc(crtc);
  3785. *dpms_mode = intel_crtc->dpms_mode;
  3786. if (!crtc->enabled) {
  3787. if (!mode)
  3788. mode = &load_detect_mode;
  3789. drm_crtc_helper_set_mode(crtc, mode, 0, 0, crtc->fb);
  3790. } else {
  3791. if (intel_crtc->dpms_mode != DRM_MODE_DPMS_ON) {
  3792. crtc_funcs = crtc->helper_private;
  3793. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  3794. }
  3795. /* Add this connector to the crtc */
  3796. encoder_funcs->mode_set(encoder, &crtc->mode, &crtc->mode);
  3797. encoder_funcs->commit(encoder);
  3798. }
  3799. /* let the connector get through one full cycle before testing */
  3800. intel_wait_for_vblank(dev);
  3801. return crtc;
  3802. }
  3803. void intel_release_load_detect_pipe(struct intel_encoder *intel_encoder,
  3804. struct drm_connector *connector, int dpms_mode)
  3805. {
  3806. struct drm_encoder *encoder = &intel_encoder->enc;
  3807. struct drm_device *dev = encoder->dev;
  3808. struct drm_crtc *crtc = encoder->crtc;
  3809. struct drm_encoder_helper_funcs *encoder_funcs = encoder->helper_private;
  3810. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  3811. if (intel_encoder->load_detect_temp) {
  3812. encoder->crtc = NULL;
  3813. connector->encoder = NULL;
  3814. intel_encoder->load_detect_temp = false;
  3815. crtc->enabled = drm_helper_crtc_in_use(crtc);
  3816. drm_helper_disable_unused_functions(dev);
  3817. }
  3818. /* Switch crtc and encoder back off if necessary */
  3819. if (crtc->enabled && dpms_mode != DRM_MODE_DPMS_ON) {
  3820. if (encoder->crtc == crtc)
  3821. encoder_funcs->dpms(encoder, dpms_mode);
  3822. crtc_funcs->dpms(crtc, dpms_mode);
  3823. }
  3824. }
  3825. /* Returns the clock of the currently programmed mode of the given pipe. */
  3826. static int intel_crtc_clock_get(struct drm_device *dev, struct drm_crtc *crtc)
  3827. {
  3828. struct drm_i915_private *dev_priv = dev->dev_private;
  3829. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3830. int pipe = intel_crtc->pipe;
  3831. u32 dpll = I915_READ((pipe == 0) ? DPLL_A : DPLL_B);
  3832. u32 fp;
  3833. intel_clock_t clock;
  3834. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  3835. fp = I915_READ((pipe == 0) ? FPA0 : FPB0);
  3836. else
  3837. fp = I915_READ((pipe == 0) ? FPA1 : FPB1);
  3838. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  3839. if (IS_PINEVIEW(dev)) {
  3840. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  3841. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  3842. } else {
  3843. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  3844. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  3845. }
  3846. if (IS_I9XX(dev)) {
  3847. if (IS_PINEVIEW(dev))
  3848. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  3849. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  3850. else
  3851. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  3852. DPLL_FPA01_P1_POST_DIV_SHIFT);
  3853. switch (dpll & DPLL_MODE_MASK) {
  3854. case DPLLB_MODE_DAC_SERIAL:
  3855. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  3856. 5 : 10;
  3857. break;
  3858. case DPLLB_MODE_LVDS:
  3859. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  3860. 7 : 14;
  3861. break;
  3862. default:
  3863. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  3864. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  3865. return 0;
  3866. }
  3867. /* XXX: Handle the 100Mhz refclk */
  3868. intel_clock(dev, 96000, &clock);
  3869. } else {
  3870. bool is_lvds = (pipe == 1) && (I915_READ(LVDS) & LVDS_PORT_EN);
  3871. if (is_lvds) {
  3872. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  3873. DPLL_FPA01_P1_POST_DIV_SHIFT);
  3874. clock.p2 = 14;
  3875. if ((dpll & PLL_REF_INPUT_MASK) ==
  3876. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  3877. /* XXX: might not be 66MHz */
  3878. intel_clock(dev, 66000, &clock);
  3879. } else
  3880. intel_clock(dev, 48000, &clock);
  3881. } else {
  3882. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  3883. clock.p1 = 2;
  3884. else {
  3885. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  3886. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  3887. }
  3888. if (dpll & PLL_P2_DIVIDE_BY_4)
  3889. clock.p2 = 4;
  3890. else
  3891. clock.p2 = 2;
  3892. intel_clock(dev, 48000, &clock);
  3893. }
  3894. }
  3895. /* XXX: It would be nice to validate the clocks, but we can't reuse
  3896. * i830PllIsValid() because it relies on the xf86_config connector
  3897. * configuration being accurate, which it isn't necessarily.
  3898. */
  3899. return clock.dot;
  3900. }
  3901. /** Returns the currently programmed mode of the given pipe. */
  3902. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  3903. struct drm_crtc *crtc)
  3904. {
  3905. struct drm_i915_private *dev_priv = dev->dev_private;
  3906. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3907. int pipe = intel_crtc->pipe;
  3908. struct drm_display_mode *mode;
  3909. int htot = I915_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  3910. int hsync = I915_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  3911. int vtot = I915_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  3912. int vsync = I915_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  3913. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  3914. if (!mode)
  3915. return NULL;
  3916. mode->clock = intel_crtc_clock_get(dev, crtc);
  3917. mode->hdisplay = (htot & 0xffff) + 1;
  3918. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  3919. mode->hsync_start = (hsync & 0xffff) + 1;
  3920. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  3921. mode->vdisplay = (vtot & 0xffff) + 1;
  3922. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  3923. mode->vsync_start = (vsync & 0xffff) + 1;
  3924. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  3925. drm_mode_set_name(mode);
  3926. drm_mode_set_crtcinfo(mode, 0);
  3927. return mode;
  3928. }
  3929. #define GPU_IDLE_TIMEOUT 500 /* ms */
  3930. /* When this timer fires, we've been idle for awhile */
  3931. static void intel_gpu_idle_timer(unsigned long arg)
  3932. {
  3933. struct drm_device *dev = (struct drm_device *)arg;
  3934. drm_i915_private_t *dev_priv = dev->dev_private;
  3935. DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
  3936. dev_priv->busy = false;
  3937. queue_work(dev_priv->wq, &dev_priv->idle_work);
  3938. }
  3939. #define CRTC_IDLE_TIMEOUT 1000 /* ms */
  3940. static void intel_crtc_idle_timer(unsigned long arg)
  3941. {
  3942. struct intel_crtc *intel_crtc = (struct intel_crtc *)arg;
  3943. struct drm_crtc *crtc = &intel_crtc->base;
  3944. drm_i915_private_t *dev_priv = crtc->dev->dev_private;
  3945. DRM_DEBUG_DRIVER("idle timer fired, downclocking\n");
  3946. intel_crtc->busy = false;
  3947. queue_work(dev_priv->wq, &dev_priv->idle_work);
  3948. }
  3949. static void intel_increase_pllclock(struct drm_crtc *crtc, bool schedule)
  3950. {
  3951. struct drm_device *dev = crtc->dev;
  3952. drm_i915_private_t *dev_priv = dev->dev_private;
  3953. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3954. int pipe = intel_crtc->pipe;
  3955. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  3956. int dpll = I915_READ(dpll_reg);
  3957. if (HAS_PCH_SPLIT(dev))
  3958. return;
  3959. if (!dev_priv->lvds_downclock_avail)
  3960. return;
  3961. if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
  3962. DRM_DEBUG_DRIVER("upclocking LVDS\n");
  3963. /* Unlock panel regs */
  3964. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  3965. PANEL_UNLOCK_REGS);
  3966. dpll &= ~DISPLAY_RATE_SELECT_FPA1;
  3967. I915_WRITE(dpll_reg, dpll);
  3968. dpll = I915_READ(dpll_reg);
  3969. intel_wait_for_vblank(dev);
  3970. dpll = I915_READ(dpll_reg);
  3971. if (dpll & DISPLAY_RATE_SELECT_FPA1)
  3972. DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
  3973. /* ...and lock them again */
  3974. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  3975. }
  3976. /* Schedule downclock */
  3977. if (schedule)
  3978. mod_timer(&intel_crtc->idle_timer, jiffies +
  3979. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  3980. }
  3981. static void intel_decrease_pllclock(struct drm_crtc *crtc)
  3982. {
  3983. struct drm_device *dev = crtc->dev;
  3984. drm_i915_private_t *dev_priv = dev->dev_private;
  3985. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3986. int pipe = intel_crtc->pipe;
  3987. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  3988. int dpll = I915_READ(dpll_reg);
  3989. if (HAS_PCH_SPLIT(dev))
  3990. return;
  3991. if (!dev_priv->lvds_downclock_avail)
  3992. return;
  3993. /*
  3994. * Since this is called by a timer, we should never get here in
  3995. * the manual case.
  3996. */
  3997. if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
  3998. DRM_DEBUG_DRIVER("downclocking LVDS\n");
  3999. /* Unlock panel regs */
  4000. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) |
  4001. PANEL_UNLOCK_REGS);
  4002. dpll |= DISPLAY_RATE_SELECT_FPA1;
  4003. I915_WRITE(dpll_reg, dpll);
  4004. dpll = I915_READ(dpll_reg);
  4005. intel_wait_for_vblank(dev);
  4006. dpll = I915_READ(dpll_reg);
  4007. if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
  4008. DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
  4009. /* ...and lock them again */
  4010. I915_WRITE(PP_CONTROL, I915_READ(PP_CONTROL) & 0x3);
  4011. }
  4012. }
  4013. /**
  4014. * intel_idle_update - adjust clocks for idleness
  4015. * @work: work struct
  4016. *
  4017. * Either the GPU or display (or both) went idle. Check the busy status
  4018. * here and adjust the CRTC and GPU clocks as necessary.
  4019. */
  4020. static void intel_idle_update(struct work_struct *work)
  4021. {
  4022. drm_i915_private_t *dev_priv = container_of(work, drm_i915_private_t,
  4023. idle_work);
  4024. struct drm_device *dev = dev_priv->dev;
  4025. struct drm_crtc *crtc;
  4026. struct intel_crtc *intel_crtc;
  4027. int enabled = 0;
  4028. if (!i915_powersave)
  4029. return;
  4030. mutex_lock(&dev->struct_mutex);
  4031. i915_update_gfx_val(dev_priv);
  4032. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4033. /* Skip inactive CRTCs */
  4034. if (!crtc->fb)
  4035. continue;
  4036. enabled++;
  4037. intel_crtc = to_intel_crtc(crtc);
  4038. if (!intel_crtc->busy)
  4039. intel_decrease_pllclock(crtc);
  4040. }
  4041. if ((enabled == 1) && (IS_I945G(dev) || IS_I945GM(dev))) {
  4042. DRM_DEBUG_DRIVER("enable memory self refresh on 945\n");
  4043. I915_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN_MASK | FW_BLC_SELF_EN);
  4044. }
  4045. mutex_unlock(&dev->struct_mutex);
  4046. }
  4047. /**
  4048. * intel_mark_busy - mark the GPU and possibly the display busy
  4049. * @dev: drm device
  4050. * @obj: object we're operating on
  4051. *
  4052. * Callers can use this function to indicate that the GPU is busy processing
  4053. * commands. If @obj matches one of the CRTC objects (i.e. it's a scanout
  4054. * buffer), we'll also mark the display as busy, so we know to increase its
  4055. * clock frequency.
  4056. */
  4057. void intel_mark_busy(struct drm_device *dev, struct drm_gem_object *obj)
  4058. {
  4059. drm_i915_private_t *dev_priv = dev->dev_private;
  4060. struct drm_crtc *crtc = NULL;
  4061. struct intel_framebuffer *intel_fb;
  4062. struct intel_crtc *intel_crtc;
  4063. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4064. return;
  4065. if (!dev_priv->busy) {
  4066. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4067. u32 fw_blc_self;
  4068. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4069. fw_blc_self = I915_READ(FW_BLC_SELF);
  4070. fw_blc_self &= ~FW_BLC_SELF_EN;
  4071. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4072. }
  4073. dev_priv->busy = true;
  4074. } else
  4075. mod_timer(&dev_priv->idle_timer, jiffies +
  4076. msecs_to_jiffies(GPU_IDLE_TIMEOUT));
  4077. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4078. if (!crtc->fb)
  4079. continue;
  4080. intel_crtc = to_intel_crtc(crtc);
  4081. intel_fb = to_intel_framebuffer(crtc->fb);
  4082. if (intel_fb->obj == obj) {
  4083. if (!intel_crtc->busy) {
  4084. if (IS_I945G(dev) || IS_I945GM(dev)) {
  4085. u32 fw_blc_self;
  4086. DRM_DEBUG_DRIVER("disable memory self refresh on 945\n");
  4087. fw_blc_self = I915_READ(FW_BLC_SELF);
  4088. fw_blc_self &= ~FW_BLC_SELF_EN;
  4089. I915_WRITE(FW_BLC_SELF, fw_blc_self | FW_BLC_SELF_EN_MASK);
  4090. }
  4091. /* Non-busy -> busy, upclock */
  4092. intel_increase_pllclock(crtc, true);
  4093. intel_crtc->busy = true;
  4094. } else {
  4095. /* Busy -> busy, put off timer */
  4096. mod_timer(&intel_crtc->idle_timer, jiffies +
  4097. msecs_to_jiffies(CRTC_IDLE_TIMEOUT));
  4098. }
  4099. }
  4100. }
  4101. }
  4102. static void intel_crtc_destroy(struct drm_crtc *crtc)
  4103. {
  4104. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4105. drm_crtc_cleanup(crtc);
  4106. kfree(intel_crtc);
  4107. }
  4108. struct intel_unpin_work {
  4109. struct work_struct work;
  4110. struct drm_device *dev;
  4111. struct drm_gem_object *old_fb_obj;
  4112. struct drm_gem_object *pending_flip_obj;
  4113. struct drm_pending_vblank_event *event;
  4114. int pending;
  4115. };
  4116. static void intel_unpin_work_fn(struct work_struct *__work)
  4117. {
  4118. struct intel_unpin_work *work =
  4119. container_of(__work, struct intel_unpin_work, work);
  4120. mutex_lock(&work->dev->struct_mutex);
  4121. i915_gem_object_unpin(work->old_fb_obj);
  4122. drm_gem_object_unreference(work->pending_flip_obj);
  4123. drm_gem_object_unreference(work->old_fb_obj);
  4124. mutex_unlock(&work->dev->struct_mutex);
  4125. kfree(work);
  4126. }
  4127. static void do_intel_finish_page_flip(struct drm_device *dev,
  4128. struct drm_crtc *crtc)
  4129. {
  4130. drm_i915_private_t *dev_priv = dev->dev_private;
  4131. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4132. struct intel_unpin_work *work;
  4133. struct drm_i915_gem_object *obj_priv;
  4134. struct drm_pending_vblank_event *e;
  4135. struct timeval now;
  4136. unsigned long flags;
  4137. /* Ignore early vblank irqs */
  4138. if (intel_crtc == NULL)
  4139. return;
  4140. spin_lock_irqsave(&dev->event_lock, flags);
  4141. work = intel_crtc->unpin_work;
  4142. if (work == NULL || !work->pending) {
  4143. spin_unlock_irqrestore(&dev->event_lock, flags);
  4144. return;
  4145. }
  4146. intel_crtc->unpin_work = NULL;
  4147. drm_vblank_put(dev, intel_crtc->pipe);
  4148. if (work->event) {
  4149. e = work->event;
  4150. do_gettimeofday(&now);
  4151. e->event.sequence = drm_vblank_count(dev, intel_crtc->pipe);
  4152. e->event.tv_sec = now.tv_sec;
  4153. e->event.tv_usec = now.tv_usec;
  4154. list_add_tail(&e->base.link,
  4155. &e->base.file_priv->event_list);
  4156. wake_up_interruptible(&e->base.file_priv->event_wait);
  4157. }
  4158. spin_unlock_irqrestore(&dev->event_lock, flags);
  4159. obj_priv = to_intel_bo(work->pending_flip_obj);
  4160. /* Initial scanout buffer will have a 0 pending flip count */
  4161. if ((atomic_read(&obj_priv->pending_flip) == 0) ||
  4162. atomic_dec_and_test(&obj_priv->pending_flip))
  4163. DRM_WAKEUP(&dev_priv->pending_flip_queue);
  4164. schedule_work(&work->work);
  4165. }
  4166. void intel_finish_page_flip(struct drm_device *dev, int pipe)
  4167. {
  4168. drm_i915_private_t *dev_priv = dev->dev_private;
  4169. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  4170. do_intel_finish_page_flip(dev, crtc);
  4171. }
  4172. void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
  4173. {
  4174. drm_i915_private_t *dev_priv = dev->dev_private;
  4175. struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
  4176. do_intel_finish_page_flip(dev, crtc);
  4177. }
  4178. void intel_prepare_page_flip(struct drm_device *dev, int plane)
  4179. {
  4180. drm_i915_private_t *dev_priv = dev->dev_private;
  4181. struct intel_crtc *intel_crtc =
  4182. to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
  4183. unsigned long flags;
  4184. spin_lock_irqsave(&dev->event_lock, flags);
  4185. if (intel_crtc->unpin_work) {
  4186. intel_crtc->unpin_work->pending = 1;
  4187. } else {
  4188. DRM_DEBUG_DRIVER("preparing flip with no unpin work?\n");
  4189. }
  4190. spin_unlock_irqrestore(&dev->event_lock, flags);
  4191. }
  4192. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  4193. struct drm_framebuffer *fb,
  4194. struct drm_pending_vblank_event *event)
  4195. {
  4196. struct drm_device *dev = crtc->dev;
  4197. struct drm_i915_private *dev_priv = dev->dev_private;
  4198. struct intel_framebuffer *intel_fb;
  4199. struct drm_i915_gem_object *obj_priv;
  4200. struct drm_gem_object *obj;
  4201. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4202. struct intel_unpin_work *work;
  4203. unsigned long flags, offset;
  4204. int pipesrc_reg = (intel_crtc->pipe == 0) ? PIPEASRC : PIPEBSRC;
  4205. int ret, pipesrc;
  4206. u32 flip_mask;
  4207. work = kzalloc(sizeof *work, GFP_KERNEL);
  4208. if (work == NULL)
  4209. return -ENOMEM;
  4210. work->event = event;
  4211. work->dev = crtc->dev;
  4212. intel_fb = to_intel_framebuffer(crtc->fb);
  4213. work->old_fb_obj = intel_fb->obj;
  4214. INIT_WORK(&work->work, intel_unpin_work_fn);
  4215. /* We borrow the event spin lock for protecting unpin_work */
  4216. spin_lock_irqsave(&dev->event_lock, flags);
  4217. if (intel_crtc->unpin_work) {
  4218. spin_unlock_irqrestore(&dev->event_lock, flags);
  4219. kfree(work);
  4220. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  4221. return -EBUSY;
  4222. }
  4223. intel_crtc->unpin_work = work;
  4224. spin_unlock_irqrestore(&dev->event_lock, flags);
  4225. intel_fb = to_intel_framebuffer(fb);
  4226. obj = intel_fb->obj;
  4227. mutex_lock(&dev->struct_mutex);
  4228. ret = intel_pin_and_fence_fb_obj(dev, obj);
  4229. if (ret != 0) {
  4230. mutex_unlock(&dev->struct_mutex);
  4231. spin_lock_irqsave(&dev->event_lock, flags);
  4232. intel_crtc->unpin_work = NULL;
  4233. spin_unlock_irqrestore(&dev->event_lock, flags);
  4234. kfree(work);
  4235. DRM_DEBUG_DRIVER("flip queue: %p pin & fence failed\n",
  4236. to_intel_bo(obj));
  4237. return ret;
  4238. }
  4239. /* Reference the objects for the scheduled work. */
  4240. drm_gem_object_reference(work->old_fb_obj);
  4241. drm_gem_object_reference(obj);
  4242. crtc->fb = fb;
  4243. i915_gem_object_flush_write_domain(obj);
  4244. drm_vblank_get(dev, intel_crtc->pipe);
  4245. obj_priv = to_intel_bo(obj);
  4246. atomic_inc(&obj_priv->pending_flip);
  4247. work->pending_flip_obj = obj;
  4248. if (intel_crtc->plane)
  4249. flip_mask = I915_DISPLAY_PLANE_B_FLIP_PENDING_INTERRUPT;
  4250. else
  4251. flip_mask = I915_DISPLAY_PLANE_A_FLIP_PENDING_INTERRUPT;
  4252. /* Wait for any previous flip to finish */
  4253. if (IS_GEN3(dev))
  4254. while (I915_READ(ISR) & flip_mask)
  4255. ;
  4256. /* Offset into the new buffer for cases of shared fbs between CRTCs */
  4257. offset = obj_priv->gtt_offset;
  4258. offset += (crtc->y * fb->pitch) + (crtc->x * (fb->bits_per_pixel) / 8);
  4259. BEGIN_LP_RING(4);
  4260. if (IS_I965G(dev)) {
  4261. OUT_RING(MI_DISPLAY_FLIP |
  4262. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4263. OUT_RING(fb->pitch);
  4264. OUT_RING(offset | obj_priv->tiling_mode);
  4265. pipesrc = I915_READ(pipesrc_reg);
  4266. OUT_RING(pipesrc & 0x0fff0fff);
  4267. } else {
  4268. OUT_RING(MI_DISPLAY_FLIP_I915 |
  4269. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  4270. OUT_RING(fb->pitch);
  4271. OUT_RING(offset);
  4272. OUT_RING(MI_NOOP);
  4273. }
  4274. ADVANCE_LP_RING();
  4275. mutex_unlock(&dev->struct_mutex);
  4276. return 0;
  4277. }
  4278. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  4279. .dpms = intel_crtc_dpms,
  4280. .mode_fixup = intel_crtc_mode_fixup,
  4281. .mode_set = intel_crtc_mode_set,
  4282. .mode_set_base = intel_pipe_set_base,
  4283. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  4284. .prepare = intel_crtc_prepare,
  4285. .commit = intel_crtc_commit,
  4286. .load_lut = intel_crtc_load_lut,
  4287. };
  4288. static const struct drm_crtc_funcs intel_crtc_funcs = {
  4289. .cursor_set = intel_crtc_cursor_set,
  4290. .cursor_move = intel_crtc_cursor_move,
  4291. .gamma_set = intel_crtc_gamma_set,
  4292. .set_config = drm_crtc_helper_set_config,
  4293. .destroy = intel_crtc_destroy,
  4294. .page_flip = intel_crtc_page_flip,
  4295. };
  4296. static void intel_crtc_init(struct drm_device *dev, int pipe)
  4297. {
  4298. drm_i915_private_t *dev_priv = dev->dev_private;
  4299. struct intel_crtc *intel_crtc;
  4300. int i;
  4301. intel_crtc = kzalloc(sizeof(struct intel_crtc) + (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  4302. if (intel_crtc == NULL)
  4303. return;
  4304. drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
  4305. drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
  4306. intel_crtc->pipe = pipe;
  4307. intel_crtc->plane = pipe;
  4308. for (i = 0; i < 256; i++) {
  4309. intel_crtc->lut_r[i] = i;
  4310. intel_crtc->lut_g[i] = i;
  4311. intel_crtc->lut_b[i] = i;
  4312. }
  4313. /* Swap pipes & planes for FBC on pre-965 */
  4314. intel_crtc->pipe = pipe;
  4315. intel_crtc->plane = pipe;
  4316. if (IS_MOBILE(dev) && (IS_I9XX(dev) && !IS_I965G(dev))) {
  4317. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  4318. intel_crtc->plane = ((pipe == 0) ? 1 : 0);
  4319. }
  4320. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  4321. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  4322. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  4323. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  4324. intel_crtc->cursor_addr = 0;
  4325. intel_crtc->dpms_mode = DRM_MODE_DPMS_OFF;
  4326. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  4327. intel_crtc->busy = false;
  4328. setup_timer(&intel_crtc->idle_timer, intel_crtc_idle_timer,
  4329. (unsigned long)intel_crtc);
  4330. }
  4331. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  4332. struct drm_file *file_priv)
  4333. {
  4334. drm_i915_private_t *dev_priv = dev->dev_private;
  4335. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  4336. struct drm_mode_object *drmmode_obj;
  4337. struct intel_crtc *crtc;
  4338. if (!dev_priv) {
  4339. DRM_ERROR("called with no initialization\n");
  4340. return -EINVAL;
  4341. }
  4342. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  4343. DRM_MODE_OBJECT_CRTC);
  4344. if (!drmmode_obj) {
  4345. DRM_ERROR("no such CRTC id\n");
  4346. return -EINVAL;
  4347. }
  4348. crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
  4349. pipe_from_crtc_id->pipe = crtc->pipe;
  4350. return 0;
  4351. }
  4352. struct drm_crtc *intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  4353. {
  4354. struct drm_crtc *crtc = NULL;
  4355. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4356. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4357. if (intel_crtc->pipe == pipe)
  4358. break;
  4359. }
  4360. return crtc;
  4361. }
  4362. static int intel_encoder_clones(struct drm_device *dev, int type_mask)
  4363. {
  4364. int index_mask = 0;
  4365. struct drm_encoder *encoder;
  4366. int entry = 0;
  4367. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  4368. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  4369. if (type_mask & intel_encoder->clone_mask)
  4370. index_mask |= (1 << entry);
  4371. entry++;
  4372. }
  4373. return index_mask;
  4374. }
  4375. static void intel_setup_outputs(struct drm_device *dev)
  4376. {
  4377. struct drm_i915_private *dev_priv = dev->dev_private;
  4378. struct drm_encoder *encoder;
  4379. intel_crt_init(dev);
  4380. /* Set up integrated LVDS */
  4381. if (IS_MOBILE(dev) && !IS_I830(dev))
  4382. intel_lvds_init(dev);
  4383. if (HAS_PCH_SPLIT(dev)) {
  4384. int found;
  4385. if (IS_MOBILE(dev) && (I915_READ(DP_A) & DP_DETECTED))
  4386. intel_dp_init(dev, DP_A);
  4387. if (I915_READ(HDMIB) & PORT_DETECTED) {
  4388. /* PCH SDVOB multiplex with HDMIB */
  4389. found = intel_sdvo_init(dev, PCH_SDVOB);
  4390. if (!found)
  4391. intel_hdmi_init(dev, HDMIB);
  4392. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  4393. intel_dp_init(dev, PCH_DP_B);
  4394. }
  4395. if (I915_READ(HDMIC) & PORT_DETECTED)
  4396. intel_hdmi_init(dev, HDMIC);
  4397. if (I915_READ(HDMID) & PORT_DETECTED)
  4398. intel_hdmi_init(dev, HDMID);
  4399. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  4400. intel_dp_init(dev, PCH_DP_C);
  4401. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  4402. intel_dp_init(dev, PCH_DP_D);
  4403. } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
  4404. bool found = false;
  4405. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4406. DRM_DEBUG_KMS("probing SDVOB\n");
  4407. found = intel_sdvo_init(dev, SDVOB);
  4408. if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
  4409. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  4410. intel_hdmi_init(dev, SDVOB);
  4411. }
  4412. if (!found && SUPPORTS_INTEGRATED_DP(dev)) {
  4413. DRM_DEBUG_KMS("probing DP_B\n");
  4414. intel_dp_init(dev, DP_B);
  4415. }
  4416. }
  4417. /* Before G4X SDVOC doesn't have its own detect register */
  4418. if (I915_READ(SDVOB) & SDVO_DETECTED) {
  4419. DRM_DEBUG_KMS("probing SDVOC\n");
  4420. found = intel_sdvo_init(dev, SDVOC);
  4421. }
  4422. if (!found && (I915_READ(SDVOC) & SDVO_DETECTED)) {
  4423. if (SUPPORTS_INTEGRATED_HDMI(dev)) {
  4424. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  4425. intel_hdmi_init(dev, SDVOC);
  4426. }
  4427. if (SUPPORTS_INTEGRATED_DP(dev)) {
  4428. DRM_DEBUG_KMS("probing DP_C\n");
  4429. intel_dp_init(dev, DP_C);
  4430. }
  4431. }
  4432. if (SUPPORTS_INTEGRATED_DP(dev) &&
  4433. (I915_READ(DP_D) & DP_DETECTED)) {
  4434. DRM_DEBUG_KMS("probing DP_D\n");
  4435. intel_dp_init(dev, DP_D);
  4436. }
  4437. } else if (IS_GEN2(dev))
  4438. intel_dvo_init(dev);
  4439. if (SUPPORTS_TV(dev))
  4440. intel_tv_init(dev);
  4441. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  4442. struct intel_encoder *intel_encoder = enc_to_intel_encoder(encoder);
  4443. encoder->possible_crtcs = intel_encoder->crtc_mask;
  4444. encoder->possible_clones = intel_encoder_clones(dev,
  4445. intel_encoder->clone_mask);
  4446. }
  4447. }
  4448. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  4449. {
  4450. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4451. drm_framebuffer_cleanup(fb);
  4452. drm_gem_object_unreference_unlocked(intel_fb->obj);
  4453. kfree(intel_fb);
  4454. }
  4455. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  4456. struct drm_file *file_priv,
  4457. unsigned int *handle)
  4458. {
  4459. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  4460. struct drm_gem_object *object = intel_fb->obj;
  4461. return drm_gem_handle_create(file_priv, object, handle);
  4462. }
  4463. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  4464. .destroy = intel_user_framebuffer_destroy,
  4465. .create_handle = intel_user_framebuffer_create_handle,
  4466. };
  4467. int intel_framebuffer_init(struct drm_device *dev,
  4468. struct intel_framebuffer *intel_fb,
  4469. struct drm_mode_fb_cmd *mode_cmd,
  4470. struct drm_gem_object *obj)
  4471. {
  4472. int ret;
  4473. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  4474. if (ret) {
  4475. DRM_ERROR("framebuffer init failed %d\n", ret);
  4476. return ret;
  4477. }
  4478. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  4479. intel_fb->obj = obj;
  4480. return 0;
  4481. }
  4482. static struct drm_framebuffer *
  4483. intel_user_framebuffer_create(struct drm_device *dev,
  4484. struct drm_file *filp,
  4485. struct drm_mode_fb_cmd *mode_cmd)
  4486. {
  4487. struct drm_gem_object *obj;
  4488. struct intel_framebuffer *intel_fb;
  4489. int ret;
  4490. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handle);
  4491. if (!obj)
  4492. return NULL;
  4493. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  4494. if (!intel_fb)
  4495. return NULL;
  4496. ret = intel_framebuffer_init(dev, intel_fb,
  4497. mode_cmd, obj);
  4498. if (ret) {
  4499. drm_gem_object_unreference_unlocked(obj);
  4500. kfree(intel_fb);
  4501. return NULL;
  4502. }
  4503. return &intel_fb->base;
  4504. }
  4505. static const struct drm_mode_config_funcs intel_mode_funcs = {
  4506. .fb_create = intel_user_framebuffer_create,
  4507. .output_poll_changed = intel_fb_output_poll_changed,
  4508. };
  4509. static struct drm_gem_object *
  4510. intel_alloc_power_context(struct drm_device *dev)
  4511. {
  4512. struct drm_gem_object *pwrctx;
  4513. int ret;
  4514. pwrctx = i915_gem_alloc_object(dev, 4096);
  4515. if (!pwrctx) {
  4516. DRM_DEBUG("failed to alloc power context, RC6 disabled\n");
  4517. return NULL;
  4518. }
  4519. mutex_lock(&dev->struct_mutex);
  4520. ret = i915_gem_object_pin(pwrctx, 4096);
  4521. if (ret) {
  4522. DRM_ERROR("failed to pin power context: %d\n", ret);
  4523. goto err_unref;
  4524. }
  4525. ret = i915_gem_object_set_to_gtt_domain(pwrctx, 1);
  4526. if (ret) {
  4527. DRM_ERROR("failed to set-domain on power context: %d\n", ret);
  4528. goto err_unpin;
  4529. }
  4530. mutex_unlock(&dev->struct_mutex);
  4531. return pwrctx;
  4532. err_unpin:
  4533. i915_gem_object_unpin(pwrctx);
  4534. err_unref:
  4535. drm_gem_object_unreference(pwrctx);
  4536. mutex_unlock(&dev->struct_mutex);
  4537. return NULL;
  4538. }
  4539. bool ironlake_set_drps(struct drm_device *dev, u8 val)
  4540. {
  4541. struct drm_i915_private *dev_priv = dev->dev_private;
  4542. u16 rgvswctl;
  4543. rgvswctl = I915_READ16(MEMSWCTL);
  4544. if (rgvswctl & MEMCTL_CMD_STS) {
  4545. DRM_DEBUG("gpu busy, RCS change rejected\n");
  4546. return false; /* still busy with another command */
  4547. }
  4548. rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
  4549. (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
  4550. I915_WRITE16(MEMSWCTL, rgvswctl);
  4551. POSTING_READ16(MEMSWCTL);
  4552. rgvswctl |= MEMCTL_CMD_STS;
  4553. I915_WRITE16(MEMSWCTL, rgvswctl);
  4554. return true;
  4555. }
  4556. void ironlake_enable_drps(struct drm_device *dev)
  4557. {
  4558. struct drm_i915_private *dev_priv = dev->dev_private;
  4559. u32 rgvmodectl = I915_READ(MEMMODECTL);
  4560. u8 fmax, fmin, fstart, vstart;
  4561. int i = 0;
  4562. /* 100ms RC evaluation intervals */
  4563. I915_WRITE(RCUPEI, 100000);
  4564. I915_WRITE(RCDNEI, 100000);
  4565. /* Set max/min thresholds to 90ms and 80ms respectively */
  4566. I915_WRITE(RCBMAXAVG, 90000);
  4567. I915_WRITE(RCBMINAVG, 80000);
  4568. I915_WRITE(MEMIHYST, 1);
  4569. /* Set up min, max, and cur for interrupt handling */
  4570. fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
  4571. fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
  4572. fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
  4573. MEMMODE_FSTART_SHIFT;
  4574. fstart = fmax;
  4575. vstart = (I915_READ(PXVFREQ_BASE + (fstart * 4)) & PXVFREQ_PX_MASK) >>
  4576. PXVFREQ_PX_SHIFT;
  4577. dev_priv->fmax = fstart; /* IPS callback will increase this */
  4578. dev_priv->fstart = fstart;
  4579. dev_priv->max_delay = fmax;
  4580. dev_priv->min_delay = fmin;
  4581. dev_priv->cur_delay = fstart;
  4582. DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n", fmax, fmin,
  4583. fstart);
  4584. I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
  4585. /*
  4586. * Interrupts will be enabled in ironlake_irq_postinstall
  4587. */
  4588. I915_WRITE(VIDSTART, vstart);
  4589. POSTING_READ(VIDSTART);
  4590. rgvmodectl |= MEMMODE_SWMODE_EN;
  4591. I915_WRITE(MEMMODECTL, rgvmodectl);
  4592. while (I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) {
  4593. if (i++ > 100) {
  4594. DRM_ERROR("stuck trying to change perf mode\n");
  4595. break;
  4596. }
  4597. msleep(1);
  4598. }
  4599. msleep(1);
  4600. ironlake_set_drps(dev, fstart);
  4601. dev_priv->last_count1 = I915_READ(0x112e4) + I915_READ(0x112e8) +
  4602. I915_READ(0x112e0);
  4603. dev_priv->last_time1 = jiffies_to_msecs(jiffies);
  4604. dev_priv->last_count2 = I915_READ(0x112f4);
  4605. getrawmonotonic(&dev_priv->last_time2);
  4606. }
  4607. void ironlake_disable_drps(struct drm_device *dev)
  4608. {
  4609. struct drm_i915_private *dev_priv = dev->dev_private;
  4610. u16 rgvswctl = I915_READ16(MEMSWCTL);
  4611. /* Ack interrupts, disable EFC interrupt */
  4612. I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
  4613. I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
  4614. I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
  4615. I915_WRITE(DEIIR, DE_PCU_EVENT);
  4616. I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
  4617. /* Go back to the starting frequency */
  4618. ironlake_set_drps(dev, dev_priv->fstart);
  4619. msleep(1);
  4620. rgvswctl |= MEMCTL_CMD_STS;
  4621. I915_WRITE(MEMSWCTL, rgvswctl);
  4622. msleep(1);
  4623. }
  4624. static unsigned long intel_pxfreq(u32 vidfreq)
  4625. {
  4626. unsigned long freq;
  4627. int div = (vidfreq & 0x3f0000) >> 16;
  4628. int post = (vidfreq & 0x3000) >> 12;
  4629. int pre = (vidfreq & 0x7);
  4630. if (!pre)
  4631. return 0;
  4632. freq = ((div * 133333) / ((1<<post) * pre));
  4633. return freq;
  4634. }
  4635. void intel_init_emon(struct drm_device *dev)
  4636. {
  4637. struct drm_i915_private *dev_priv = dev->dev_private;
  4638. u32 lcfuse;
  4639. u8 pxw[16];
  4640. int i;
  4641. /* Disable to program */
  4642. I915_WRITE(ECR, 0);
  4643. POSTING_READ(ECR);
  4644. /* Program energy weights for various events */
  4645. I915_WRITE(SDEW, 0x15040d00);
  4646. I915_WRITE(CSIEW0, 0x007f0000);
  4647. I915_WRITE(CSIEW1, 0x1e220004);
  4648. I915_WRITE(CSIEW2, 0x04000004);
  4649. for (i = 0; i < 5; i++)
  4650. I915_WRITE(PEW + (i * 4), 0);
  4651. for (i = 0; i < 3; i++)
  4652. I915_WRITE(DEW + (i * 4), 0);
  4653. /* Program P-state weights to account for frequency power adjustment */
  4654. for (i = 0; i < 16; i++) {
  4655. u32 pxvidfreq = I915_READ(PXVFREQ_BASE + (i * 4));
  4656. unsigned long freq = intel_pxfreq(pxvidfreq);
  4657. unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
  4658. PXVFREQ_PX_SHIFT;
  4659. unsigned long val;
  4660. val = vid * vid;
  4661. val *= (freq / 1000);
  4662. val *= 255;
  4663. val /= (127*127*900);
  4664. if (val > 0xff)
  4665. DRM_ERROR("bad pxval: %ld\n", val);
  4666. pxw[i] = val;
  4667. }
  4668. /* Render standby states get 0 weight */
  4669. pxw[14] = 0;
  4670. pxw[15] = 0;
  4671. for (i = 0; i < 4; i++) {
  4672. u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
  4673. (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
  4674. I915_WRITE(PXW + (i * 4), val);
  4675. }
  4676. /* Adjust magic regs to magic values (more experimental results) */
  4677. I915_WRITE(OGW0, 0);
  4678. I915_WRITE(OGW1, 0);
  4679. I915_WRITE(EG0, 0x00007f00);
  4680. I915_WRITE(EG1, 0x0000000e);
  4681. I915_WRITE(EG2, 0x000e0000);
  4682. I915_WRITE(EG3, 0x68000300);
  4683. I915_WRITE(EG4, 0x42000000);
  4684. I915_WRITE(EG5, 0x00140031);
  4685. I915_WRITE(EG6, 0);
  4686. I915_WRITE(EG7, 0);
  4687. for (i = 0; i < 8; i++)
  4688. I915_WRITE(PXWL + (i * 4), 0);
  4689. /* Enable PMON + select events */
  4690. I915_WRITE(ECR, 0x80000019);
  4691. lcfuse = I915_READ(LCFUSE02);
  4692. dev_priv->corr = (lcfuse & LCFUSE_HIV_MASK);
  4693. }
  4694. void intel_init_clock_gating(struct drm_device *dev)
  4695. {
  4696. struct drm_i915_private *dev_priv = dev->dev_private;
  4697. /*
  4698. * Disable clock gating reported to work incorrectly according to the
  4699. * specs, but enable as much else as we can.
  4700. */
  4701. if (HAS_PCH_SPLIT(dev)) {
  4702. uint32_t dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE;
  4703. if (IS_IRONLAKE(dev)) {
  4704. /* Required for FBC */
  4705. dspclk_gate |= DPFDUNIT_CLOCK_GATE_DISABLE;
  4706. /* Required for CxSR */
  4707. dspclk_gate |= DPARBUNIT_CLOCK_GATE_DISABLE;
  4708. I915_WRITE(PCH_3DCGDIS0,
  4709. MARIUNIT_CLOCK_GATE_DISABLE |
  4710. SVSMUNIT_CLOCK_GATE_DISABLE);
  4711. }
  4712. I915_WRITE(PCH_DSPCLK_GATE_D, dspclk_gate);
  4713. /*
  4714. * According to the spec the following bits should be set in
  4715. * order to enable memory self-refresh
  4716. * The bit 22/21 of 0x42004
  4717. * The bit 5 of 0x42020
  4718. * The bit 15 of 0x45000
  4719. */
  4720. if (IS_IRONLAKE(dev)) {
  4721. I915_WRITE(ILK_DISPLAY_CHICKEN2,
  4722. (I915_READ(ILK_DISPLAY_CHICKEN2) |
  4723. ILK_DPARB_GATE | ILK_VSDPFD_FULL));
  4724. I915_WRITE(ILK_DSPCLK_GATE,
  4725. (I915_READ(ILK_DSPCLK_GATE) |
  4726. ILK_DPARB_CLK_GATE));
  4727. I915_WRITE(DISP_ARB_CTL,
  4728. (I915_READ(DISP_ARB_CTL) |
  4729. DISP_FBC_WM_DIS));
  4730. }
  4731. return;
  4732. } else if (IS_G4X(dev)) {
  4733. uint32_t dspclk_gate;
  4734. I915_WRITE(RENCLK_GATE_D1, 0);
  4735. I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
  4736. GS_UNIT_CLOCK_GATE_DISABLE |
  4737. CL_UNIT_CLOCK_GATE_DISABLE);
  4738. I915_WRITE(RAMCLK_GATE_D, 0);
  4739. dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
  4740. OVRUNIT_CLOCK_GATE_DISABLE |
  4741. OVCUNIT_CLOCK_GATE_DISABLE;
  4742. if (IS_GM45(dev))
  4743. dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
  4744. I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
  4745. } else if (IS_I965GM(dev)) {
  4746. I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
  4747. I915_WRITE(RENCLK_GATE_D2, 0);
  4748. I915_WRITE(DSPCLK_GATE_D, 0);
  4749. I915_WRITE(RAMCLK_GATE_D, 0);
  4750. I915_WRITE16(DEUC, 0);
  4751. } else if (IS_I965G(dev)) {
  4752. I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
  4753. I965_RCC_CLOCK_GATE_DISABLE |
  4754. I965_RCPB_CLOCK_GATE_DISABLE |
  4755. I965_ISC_CLOCK_GATE_DISABLE |
  4756. I965_FBC_CLOCK_GATE_DISABLE);
  4757. I915_WRITE(RENCLK_GATE_D2, 0);
  4758. } else if (IS_I9XX(dev)) {
  4759. u32 dstate = I915_READ(D_STATE);
  4760. dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
  4761. DSTATE_DOT_CLOCK_GATING;
  4762. I915_WRITE(D_STATE, dstate);
  4763. } else if (IS_I85X(dev) || IS_I865G(dev)) {
  4764. I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
  4765. } else if (IS_I830(dev)) {
  4766. I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
  4767. }
  4768. /*
  4769. * GPU can automatically power down the render unit if given a page
  4770. * to save state.
  4771. */
  4772. if (I915_HAS_RC6(dev) && drm_core_check_feature(dev, DRIVER_MODESET)) {
  4773. struct drm_i915_gem_object *obj_priv = NULL;
  4774. if (dev_priv->pwrctx) {
  4775. obj_priv = to_intel_bo(dev_priv->pwrctx);
  4776. } else {
  4777. struct drm_gem_object *pwrctx;
  4778. pwrctx = intel_alloc_power_context(dev);
  4779. if (pwrctx) {
  4780. dev_priv->pwrctx = pwrctx;
  4781. obj_priv = to_intel_bo(pwrctx);
  4782. }
  4783. }
  4784. if (obj_priv) {
  4785. I915_WRITE(PWRCTXA, obj_priv->gtt_offset | PWRCTX_EN);
  4786. I915_WRITE(MCHBAR_RENDER_STANDBY,
  4787. I915_READ(MCHBAR_RENDER_STANDBY) & ~RCX_SW_EXIT);
  4788. }
  4789. }
  4790. }
  4791. /* Set up chip specific display functions */
  4792. static void intel_init_display(struct drm_device *dev)
  4793. {
  4794. struct drm_i915_private *dev_priv = dev->dev_private;
  4795. /* We always want a DPMS function */
  4796. if (HAS_PCH_SPLIT(dev))
  4797. dev_priv->display.dpms = ironlake_crtc_dpms;
  4798. else
  4799. dev_priv->display.dpms = i9xx_crtc_dpms;
  4800. if (I915_HAS_FBC(dev)) {
  4801. if (IS_GM45(dev)) {
  4802. dev_priv->display.fbc_enabled = g4x_fbc_enabled;
  4803. dev_priv->display.enable_fbc = g4x_enable_fbc;
  4804. dev_priv->display.disable_fbc = g4x_disable_fbc;
  4805. } else if (IS_I965GM(dev)) {
  4806. dev_priv->display.fbc_enabled = i8xx_fbc_enabled;
  4807. dev_priv->display.enable_fbc = i8xx_enable_fbc;
  4808. dev_priv->display.disable_fbc = i8xx_disable_fbc;
  4809. }
  4810. /* 855GM needs testing */
  4811. }
  4812. /* Returns the core display clock speed */
  4813. if (IS_I945G(dev) || (IS_G33(dev) && ! IS_PINEVIEW_M(dev)))
  4814. dev_priv->display.get_display_clock_speed =
  4815. i945_get_display_clock_speed;
  4816. else if (IS_I915G(dev))
  4817. dev_priv->display.get_display_clock_speed =
  4818. i915_get_display_clock_speed;
  4819. else if (IS_I945GM(dev) || IS_845G(dev) || IS_PINEVIEW_M(dev))
  4820. dev_priv->display.get_display_clock_speed =
  4821. i9xx_misc_get_display_clock_speed;
  4822. else if (IS_I915GM(dev))
  4823. dev_priv->display.get_display_clock_speed =
  4824. i915gm_get_display_clock_speed;
  4825. else if (IS_I865G(dev))
  4826. dev_priv->display.get_display_clock_speed =
  4827. i865_get_display_clock_speed;
  4828. else if (IS_I85X(dev))
  4829. dev_priv->display.get_display_clock_speed =
  4830. i855_get_display_clock_speed;
  4831. else /* 852, 830 */
  4832. dev_priv->display.get_display_clock_speed =
  4833. i830_get_display_clock_speed;
  4834. /* For FIFO watermark updates */
  4835. if (HAS_PCH_SPLIT(dev)) {
  4836. if (IS_IRONLAKE(dev)) {
  4837. if (I915_READ(MLTR_ILK) & ILK_SRLT_MASK)
  4838. dev_priv->display.update_wm = ironlake_update_wm;
  4839. else {
  4840. DRM_DEBUG_KMS("Failed to get proper latency. "
  4841. "Disable CxSR\n");
  4842. dev_priv->display.update_wm = NULL;
  4843. }
  4844. } else
  4845. dev_priv->display.update_wm = NULL;
  4846. } else if (IS_PINEVIEW(dev)) {
  4847. if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
  4848. dev_priv->is_ddr3,
  4849. dev_priv->fsb_freq,
  4850. dev_priv->mem_freq)) {
  4851. DRM_INFO("failed to find known CxSR latency "
  4852. "(found ddr%s fsb freq %d, mem freq %d), "
  4853. "disabling CxSR\n",
  4854. (dev_priv->is_ddr3 == 1) ? "3": "2",
  4855. dev_priv->fsb_freq, dev_priv->mem_freq);
  4856. /* Disable CxSR and never update its watermark again */
  4857. pineview_disable_cxsr(dev);
  4858. dev_priv->display.update_wm = NULL;
  4859. } else
  4860. dev_priv->display.update_wm = pineview_update_wm;
  4861. } else if (IS_G4X(dev))
  4862. dev_priv->display.update_wm = g4x_update_wm;
  4863. else if (IS_I965G(dev))
  4864. dev_priv->display.update_wm = i965_update_wm;
  4865. else if (IS_I9XX(dev)) {
  4866. dev_priv->display.update_wm = i9xx_update_wm;
  4867. dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
  4868. } else if (IS_I85X(dev)) {
  4869. dev_priv->display.update_wm = i9xx_update_wm;
  4870. dev_priv->display.get_fifo_size = i85x_get_fifo_size;
  4871. } else {
  4872. dev_priv->display.update_wm = i830_update_wm;
  4873. if (IS_845G(dev))
  4874. dev_priv->display.get_fifo_size = i845_get_fifo_size;
  4875. else
  4876. dev_priv->display.get_fifo_size = i830_get_fifo_size;
  4877. }
  4878. }
  4879. /*
  4880. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  4881. * resume, or other times. This quirk makes sure that's the case for
  4882. * affected systems.
  4883. */
  4884. static void quirk_pipea_force (struct drm_device *dev)
  4885. {
  4886. struct drm_i915_private *dev_priv = dev->dev_private;
  4887. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  4888. DRM_DEBUG_DRIVER("applying pipe a force quirk\n");
  4889. }
  4890. struct intel_quirk {
  4891. int device;
  4892. int subsystem_vendor;
  4893. int subsystem_device;
  4894. void (*hook)(struct drm_device *dev);
  4895. };
  4896. struct intel_quirk intel_quirks[] = {
  4897. /* HP Compaq 2730p needs pipe A force quirk (LP: #291555) */
  4898. { 0x2a42, 0x103c, 0x30eb, quirk_pipea_force },
  4899. /* HP Mini needs pipe A force quirk (LP: #322104) */
  4900. { 0x27ae,0x103c, 0x361a, quirk_pipea_force },
  4901. /* Thinkpad R31 needs pipe A force quirk */
  4902. { 0x3577, 0x1014, 0x0505, quirk_pipea_force },
  4903. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  4904. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  4905. /* ThinkPad X30 needs pipe A force quirk (LP: #304614) */
  4906. { 0x3577, 0x1014, 0x0513, quirk_pipea_force },
  4907. /* ThinkPad X40 needs pipe A force quirk */
  4908. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  4909. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  4910. /* 855 & before need to leave pipe A & dpll A up */
  4911. { 0x3582, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  4912. { 0x2562, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  4913. };
  4914. static void intel_init_quirks(struct drm_device *dev)
  4915. {
  4916. struct pci_dev *d = dev->pdev;
  4917. int i;
  4918. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  4919. struct intel_quirk *q = &intel_quirks[i];
  4920. if (d->device == q->device &&
  4921. (d->subsystem_vendor == q->subsystem_vendor ||
  4922. q->subsystem_vendor == PCI_ANY_ID) &&
  4923. (d->subsystem_device == q->subsystem_device ||
  4924. q->subsystem_device == PCI_ANY_ID))
  4925. q->hook(dev);
  4926. }
  4927. }
  4928. void intel_modeset_init(struct drm_device *dev)
  4929. {
  4930. struct drm_i915_private *dev_priv = dev->dev_private;
  4931. int i;
  4932. drm_mode_config_init(dev);
  4933. dev->mode_config.min_width = 0;
  4934. dev->mode_config.min_height = 0;
  4935. dev->mode_config.funcs = (void *)&intel_mode_funcs;
  4936. intel_init_quirks(dev);
  4937. intel_init_display(dev);
  4938. if (IS_I965G(dev)) {
  4939. dev->mode_config.max_width = 8192;
  4940. dev->mode_config.max_height = 8192;
  4941. } else if (IS_I9XX(dev)) {
  4942. dev->mode_config.max_width = 4096;
  4943. dev->mode_config.max_height = 4096;
  4944. } else {
  4945. dev->mode_config.max_width = 2048;
  4946. dev->mode_config.max_height = 2048;
  4947. }
  4948. /* set memory base */
  4949. if (IS_I9XX(dev))
  4950. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 2);
  4951. else
  4952. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 0);
  4953. if (IS_MOBILE(dev) || IS_I9XX(dev))
  4954. dev_priv->num_pipe = 2;
  4955. else
  4956. dev_priv->num_pipe = 1;
  4957. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  4958. dev_priv->num_pipe, dev_priv->num_pipe > 1 ? "s" : "");
  4959. for (i = 0; i < dev_priv->num_pipe; i++) {
  4960. intel_crtc_init(dev, i);
  4961. }
  4962. intel_setup_outputs(dev);
  4963. intel_init_clock_gating(dev);
  4964. if (IS_IRONLAKE_M(dev)) {
  4965. ironlake_enable_drps(dev);
  4966. intel_init_emon(dev);
  4967. }
  4968. INIT_WORK(&dev_priv->idle_work, intel_idle_update);
  4969. setup_timer(&dev_priv->idle_timer, intel_gpu_idle_timer,
  4970. (unsigned long)dev);
  4971. intel_setup_overlay(dev);
  4972. }
  4973. void intel_modeset_cleanup(struct drm_device *dev)
  4974. {
  4975. struct drm_i915_private *dev_priv = dev->dev_private;
  4976. struct drm_crtc *crtc;
  4977. struct intel_crtc *intel_crtc;
  4978. mutex_lock(&dev->struct_mutex);
  4979. drm_kms_helper_poll_fini(dev);
  4980. intel_fbdev_fini(dev);
  4981. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  4982. /* Skip inactive CRTCs */
  4983. if (!crtc->fb)
  4984. continue;
  4985. intel_crtc = to_intel_crtc(crtc);
  4986. intel_increase_pllclock(crtc, false);
  4987. del_timer_sync(&intel_crtc->idle_timer);
  4988. }
  4989. del_timer_sync(&dev_priv->idle_timer);
  4990. if (dev_priv->display.disable_fbc)
  4991. dev_priv->display.disable_fbc(dev);
  4992. if (dev_priv->pwrctx) {
  4993. struct drm_i915_gem_object *obj_priv;
  4994. obj_priv = to_intel_bo(dev_priv->pwrctx);
  4995. I915_WRITE(PWRCTXA, obj_priv->gtt_offset &~ PWRCTX_EN);
  4996. I915_READ(PWRCTXA);
  4997. i915_gem_object_unpin(dev_priv->pwrctx);
  4998. drm_gem_object_unreference(dev_priv->pwrctx);
  4999. }
  5000. if (IS_IRONLAKE_M(dev))
  5001. ironlake_disable_drps(dev);
  5002. mutex_unlock(&dev->struct_mutex);
  5003. drm_mode_config_cleanup(dev);
  5004. }
  5005. /*
  5006. * Return which encoder is currently attached for connector.
  5007. */
  5008. struct drm_encoder *intel_attached_encoder (struct drm_connector *connector)
  5009. {
  5010. struct drm_mode_object *obj;
  5011. struct drm_encoder *encoder;
  5012. int i;
  5013. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  5014. if (connector->encoder_ids[i] == 0)
  5015. break;
  5016. obj = drm_mode_object_find(connector->dev,
  5017. connector->encoder_ids[i],
  5018. DRM_MODE_OBJECT_ENCODER);
  5019. if (!obj)
  5020. continue;
  5021. encoder = obj_to_encoder(obj);
  5022. return encoder;
  5023. }
  5024. return NULL;
  5025. }
  5026. /*
  5027. * set vga decode state - true == enable VGA decode
  5028. */
  5029. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  5030. {
  5031. struct drm_i915_private *dev_priv = dev->dev_private;
  5032. u16 gmch_ctrl;
  5033. pci_read_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, &gmch_ctrl);
  5034. if (state)
  5035. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  5036. else
  5037. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  5038. pci_write_config_word(dev_priv->bridge_dev, INTEL_GMCH_CTRL, gmch_ctrl);
  5039. return 0;
  5040. }