drm_crtc.c 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080
  1. /*
  2. * Copyright (c) 2006-2008 Intel Corporation
  3. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  4. * Copyright (c) 2008 Red Hat Inc.
  5. *
  6. * DRM core CRTC related functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Keith Packard
  28. * Eric Anholt <eric@anholt.net>
  29. * Dave Airlie <airlied@linux.ie>
  30. * Jesse Barnes <jesse.barnes@intel.com>
  31. */
  32. #include <linux/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/drm_fourcc.h>
  40. #include <drm/drm_modeset_lock.h>
  41. #include <drm/drm_atomic.h>
  42. #include "drm_crtc_internal.h"
  43. #include "drm_internal.h"
  44. static struct drm_framebuffer *
  45. internal_framebuffer_create(struct drm_device *dev,
  46. const struct drm_mode_fb_cmd2 *r,
  47. struct drm_file *file_priv);
  48. /* Avoid boilerplate. I'm tired of typing. */
  49. #define DRM_ENUM_NAME_FN(fnname, list) \
  50. const char *fnname(int val) \
  51. { \
  52. int i; \
  53. for (i = 0; i < ARRAY_SIZE(list); i++) { \
  54. if (list[i].type == val) \
  55. return list[i].name; \
  56. } \
  57. return "(unknown)"; \
  58. }
  59. /*
  60. * Global properties
  61. */
  62. static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
  63. { DRM_MODE_DPMS_ON, "On" },
  64. { DRM_MODE_DPMS_STANDBY, "Standby" },
  65. { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  66. { DRM_MODE_DPMS_OFF, "Off" }
  67. };
  68. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  69. static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
  70. { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  71. { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  72. { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  73. };
  74. /*
  75. * Optional properties
  76. */
  77. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
  78. { DRM_MODE_SCALE_NONE, "None" },
  79. { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  80. { DRM_MODE_SCALE_CENTER, "Center" },
  81. { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  82. };
  83. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  84. { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  85. { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  86. { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  87. };
  88. /*
  89. * Non-global properties, but "required" for certain connectors.
  90. */
  91. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
  92. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  93. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  94. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  95. };
  96. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  97. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
  98. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  99. { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
  100. { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
  101. };
  102. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  103. drm_dvi_i_subconnector_enum_list)
  104. static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
  105. { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  106. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  107. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  108. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  109. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  110. };
  111. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  112. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
  113. { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
  114. { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  115. { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
  116. { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  117. { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
  118. };
  119. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  120. drm_tv_subconnector_enum_list)
  121. static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  122. { DRM_MODE_DIRTY_OFF, "Off" },
  123. { DRM_MODE_DIRTY_ON, "On" },
  124. { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  125. };
  126. struct drm_conn_prop_enum_list {
  127. int type;
  128. const char *name;
  129. struct ida ida;
  130. };
  131. /*
  132. * Connector and encoder types.
  133. */
  134. static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
  135. { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  136. { DRM_MODE_CONNECTOR_VGA, "VGA" },
  137. { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  138. { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  139. { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  140. { DRM_MODE_CONNECTOR_Composite, "Composite" },
  141. { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  142. { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  143. { DRM_MODE_CONNECTOR_Component, "Component" },
  144. { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  145. { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  146. { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  147. { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  148. { DRM_MODE_CONNECTOR_TV, "TV" },
  149. { DRM_MODE_CONNECTOR_eDP, "eDP" },
  150. { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  151. { DRM_MODE_CONNECTOR_DSI, "DSI" },
  152. { DRM_MODE_CONNECTOR_DPI, "DPI" },
  153. };
  154. static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
  155. { DRM_MODE_ENCODER_NONE, "None" },
  156. { DRM_MODE_ENCODER_DAC, "DAC" },
  157. { DRM_MODE_ENCODER_TMDS, "TMDS" },
  158. { DRM_MODE_ENCODER_LVDS, "LVDS" },
  159. { DRM_MODE_ENCODER_TVDAC, "TV" },
  160. { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  161. { DRM_MODE_ENCODER_DSI, "DSI" },
  162. { DRM_MODE_ENCODER_DPMST, "DP MST" },
  163. { DRM_MODE_ENCODER_DPI, "DPI" },
  164. };
  165. static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
  166. { SubPixelUnknown, "Unknown" },
  167. { SubPixelHorizontalRGB, "Horizontal RGB" },
  168. { SubPixelHorizontalBGR, "Horizontal BGR" },
  169. { SubPixelVerticalRGB, "Vertical RGB" },
  170. { SubPixelVerticalBGR, "Vertical BGR" },
  171. { SubPixelNone, "None" },
  172. };
  173. void drm_connector_ida_init(void)
  174. {
  175. int i;
  176. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  177. ida_init(&drm_connector_enum_list[i].ida);
  178. }
  179. void drm_connector_ida_destroy(void)
  180. {
  181. int i;
  182. for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  183. ida_destroy(&drm_connector_enum_list[i].ida);
  184. }
  185. /**
  186. * drm_get_connector_status_name - return a string for connector status
  187. * @status: connector status to compute name of
  188. *
  189. * In contrast to the other drm_get_*_name functions this one here returns a
  190. * const pointer and hence is threadsafe.
  191. */
  192. const char *drm_get_connector_status_name(enum drm_connector_status status)
  193. {
  194. if (status == connector_status_connected)
  195. return "connected";
  196. else if (status == connector_status_disconnected)
  197. return "disconnected";
  198. else
  199. return "unknown";
  200. }
  201. EXPORT_SYMBOL(drm_get_connector_status_name);
  202. /**
  203. * drm_get_subpixel_order_name - return a string for a given subpixel enum
  204. * @order: enum of subpixel_order
  205. *
  206. * Note you could abuse this and return something out of bounds, but that
  207. * would be a caller error. No unscrubbed user data should make it here.
  208. */
  209. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  210. {
  211. return drm_subpixel_enum_list[order].name;
  212. }
  213. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  214. static char printable_char(int c)
  215. {
  216. return isascii(c) && isprint(c) ? c : '?';
  217. }
  218. /**
  219. * drm_get_format_name - return a string for drm fourcc format
  220. * @format: format to compute name of
  221. *
  222. * Note that the buffer used by this function is globally shared and owned by
  223. * the function itself.
  224. *
  225. * FIXME: This isn't really multithreading safe.
  226. */
  227. const char *drm_get_format_name(uint32_t format)
  228. {
  229. static char buf[32];
  230. snprintf(buf, sizeof(buf),
  231. "%c%c%c%c %s-endian (0x%08x)",
  232. printable_char(format & 0xff),
  233. printable_char((format >> 8) & 0xff),
  234. printable_char((format >> 16) & 0xff),
  235. printable_char((format >> 24) & 0x7f),
  236. format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
  237. format);
  238. return buf;
  239. }
  240. EXPORT_SYMBOL(drm_get_format_name);
  241. /*
  242. * Internal function to assign a slot in the object idr and optionally
  243. * register the object into the idr.
  244. */
  245. static int drm_mode_object_get_reg(struct drm_device *dev,
  246. struct drm_mode_object *obj,
  247. uint32_t obj_type,
  248. bool register_obj,
  249. void (*obj_free_cb)(struct kref *kref))
  250. {
  251. int ret;
  252. mutex_lock(&dev->mode_config.idr_mutex);
  253. ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
  254. if (ret >= 0) {
  255. /*
  256. * Set up the object linking under the protection of the idr
  257. * lock so that other users can't see inconsistent state.
  258. */
  259. obj->id = ret;
  260. obj->type = obj_type;
  261. if (obj_free_cb) {
  262. obj->free_cb = obj_free_cb;
  263. kref_init(&obj->refcount);
  264. }
  265. }
  266. mutex_unlock(&dev->mode_config.idr_mutex);
  267. return ret < 0 ? ret : 0;
  268. }
  269. /**
  270. * drm_mode_object_get - allocate a new modeset identifier
  271. * @dev: DRM device
  272. * @obj: object pointer, used to generate unique ID
  273. * @obj_type: object type
  274. *
  275. * Create a unique identifier based on @ptr in @dev's identifier space. Used
  276. * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
  277. * modeset identifiers are _not_ reference counted. Hence don't use this for
  278. * reference counted modeset objects like framebuffers.
  279. *
  280. * Returns:
  281. * Zero on success, error code on failure.
  282. */
  283. int drm_mode_object_get(struct drm_device *dev,
  284. struct drm_mode_object *obj, uint32_t obj_type)
  285. {
  286. return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
  287. }
  288. static void drm_mode_object_register(struct drm_device *dev,
  289. struct drm_mode_object *obj)
  290. {
  291. mutex_lock(&dev->mode_config.idr_mutex);
  292. idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
  293. mutex_unlock(&dev->mode_config.idr_mutex);
  294. }
  295. /**
  296. * drm_mode_object_unregister - free a modeset identifer
  297. * @dev: DRM device
  298. * @object: object to free
  299. *
  300. * Free @id from @dev's unique identifier pool.
  301. * This function can be called multiple times, and guards against
  302. * multiple removals.
  303. * These modeset identifiers are _not_ reference counted. Hence don't use this
  304. * for reference counted modeset objects like framebuffers.
  305. */
  306. void drm_mode_object_unregister(struct drm_device *dev,
  307. struct drm_mode_object *object)
  308. {
  309. mutex_lock(&dev->mode_config.idr_mutex);
  310. if (object->id) {
  311. idr_remove(&dev->mode_config.crtc_idr, object->id);
  312. object->id = 0;
  313. }
  314. mutex_unlock(&dev->mode_config.idr_mutex);
  315. }
  316. static struct drm_mode_object *_object_find(struct drm_device *dev,
  317. uint32_t id, uint32_t type)
  318. {
  319. struct drm_mode_object *obj = NULL;
  320. mutex_lock(&dev->mode_config.idr_mutex);
  321. obj = idr_find(&dev->mode_config.crtc_idr, id);
  322. if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  323. obj = NULL;
  324. if (obj && obj->id != id)
  325. obj = NULL;
  326. if (obj && obj->free_cb) {
  327. if (!kref_get_unless_zero(&obj->refcount))
  328. obj = NULL;
  329. }
  330. mutex_unlock(&dev->mode_config.idr_mutex);
  331. return obj;
  332. }
  333. /**
  334. * drm_mode_object_find - look up a drm object with static lifetime
  335. * @dev: drm device
  336. * @id: id of the mode object
  337. * @type: type of the mode object
  338. *
  339. * This function is used to look up a modeset object. It will acquire a
  340. * reference for reference counted objects. This reference must be dropped again
  341. * by callind drm_mode_object_unreference().
  342. */
  343. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  344. uint32_t id, uint32_t type)
  345. {
  346. struct drm_mode_object *obj = NULL;
  347. obj = _object_find(dev, id, type);
  348. return obj;
  349. }
  350. EXPORT_SYMBOL(drm_mode_object_find);
  351. /**
  352. * drm_mode_object_unreference - decr the object refcnt
  353. * @obj: mode_object
  354. *
  355. * This functions decrements the object's refcount if it is a refcounted modeset
  356. * object. It is a no-op on any other object. This is used to drop references
  357. * acquired with drm_mode_object_reference().
  358. */
  359. void drm_mode_object_unreference(struct drm_mode_object *obj)
  360. {
  361. if (obj->free_cb) {
  362. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
  363. kref_put(&obj->refcount, obj->free_cb);
  364. }
  365. }
  366. EXPORT_SYMBOL(drm_mode_object_unreference);
  367. /**
  368. * drm_mode_object_reference - incr the object refcnt
  369. * @obj: mode_object
  370. *
  371. * This functions increments the object's refcount if it is a refcounted modeset
  372. * object. It is a no-op on any other object. References should be dropped again
  373. * by calling drm_mode_object_unreference().
  374. */
  375. void drm_mode_object_reference(struct drm_mode_object *obj)
  376. {
  377. if (obj->free_cb) {
  378. DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
  379. kref_get(&obj->refcount);
  380. }
  381. }
  382. EXPORT_SYMBOL(drm_mode_object_reference);
  383. static void drm_framebuffer_free(struct kref *kref)
  384. {
  385. struct drm_framebuffer *fb =
  386. container_of(kref, struct drm_framebuffer, base.refcount);
  387. struct drm_device *dev = fb->dev;
  388. /*
  389. * The lookup idr holds a weak reference, which has not necessarily been
  390. * removed at this point. Check for that.
  391. */
  392. drm_mode_object_unregister(dev, &fb->base);
  393. fb->funcs->destroy(fb);
  394. }
  395. /**
  396. * drm_framebuffer_init - initialize a framebuffer
  397. * @dev: DRM device
  398. * @fb: framebuffer to be initialized
  399. * @funcs: ... with these functions
  400. *
  401. * Allocates an ID for the framebuffer's parent mode object, sets its mode
  402. * functions & device file and adds it to the master fd list.
  403. *
  404. * IMPORTANT:
  405. * This functions publishes the fb and makes it available for concurrent access
  406. * by other users. Which means by this point the fb _must_ be fully set up -
  407. * since all the fb attributes are invariant over its lifetime, no further
  408. * locking but only correct reference counting is required.
  409. *
  410. * Returns:
  411. * Zero on success, error code on failure.
  412. */
  413. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  414. const struct drm_framebuffer_funcs *funcs)
  415. {
  416. int ret;
  417. INIT_LIST_HEAD(&fb->filp_head);
  418. fb->dev = dev;
  419. fb->funcs = funcs;
  420. ret = drm_mode_object_get_reg(dev, &fb->base, DRM_MODE_OBJECT_FB,
  421. false, drm_framebuffer_free);
  422. if (ret)
  423. goto out;
  424. mutex_lock(&dev->mode_config.fb_lock);
  425. dev->mode_config.num_fb++;
  426. list_add(&fb->head, &dev->mode_config.fb_list);
  427. mutex_unlock(&dev->mode_config.fb_lock);
  428. drm_mode_object_register(dev, &fb->base);
  429. out:
  430. return ret;
  431. }
  432. EXPORT_SYMBOL(drm_framebuffer_init);
  433. /**
  434. * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  435. * @dev: drm device
  436. * @id: id of the fb object
  437. *
  438. * If successful, this grabs an additional reference to the framebuffer -
  439. * callers need to make sure to eventually unreference the returned framebuffer
  440. * again, using @drm_framebuffer_unreference.
  441. */
  442. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  443. uint32_t id)
  444. {
  445. struct drm_mode_object *obj;
  446. struct drm_framebuffer *fb = NULL;
  447. obj = _object_find(dev, id, DRM_MODE_OBJECT_FB);
  448. if (obj)
  449. fb = obj_to_fb(obj);
  450. return fb;
  451. }
  452. EXPORT_SYMBOL(drm_framebuffer_lookup);
  453. /**
  454. * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  455. * @fb: fb to unregister
  456. *
  457. * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  458. * those used for fbdev. Note that the caller must hold a reference of it's own,
  459. * i.e. the object may not be destroyed through this call (since it'll lead to a
  460. * locking inversion).
  461. */
  462. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  463. {
  464. struct drm_device *dev;
  465. if (!fb)
  466. return;
  467. dev = fb->dev;
  468. /* Mark fb as reaped and drop idr ref. */
  469. drm_mode_object_unregister(dev, &fb->base);
  470. }
  471. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  472. /**
  473. * drm_framebuffer_cleanup - remove a framebuffer object
  474. * @fb: framebuffer to remove
  475. *
  476. * Cleanup framebuffer. This function is intended to be used from the drivers
  477. * ->destroy callback. It can also be used to clean up driver private
  478. * framebuffers embedded into a larger structure.
  479. *
  480. * Note that this function does not remove the fb from active usuage - if it is
  481. * still used anywhere, hilarity can ensue since userspace could call getfb on
  482. * the id and get back -EINVAL. Obviously no concern at driver unload time.
  483. *
  484. * Also, the framebuffer will not be removed from the lookup idr - for
  485. * user-created framebuffers this will happen in in the rmfb ioctl. For
  486. * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  487. * drm_framebuffer_unregister_private.
  488. */
  489. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  490. {
  491. struct drm_device *dev = fb->dev;
  492. mutex_lock(&dev->mode_config.fb_lock);
  493. list_del(&fb->head);
  494. dev->mode_config.num_fb--;
  495. mutex_unlock(&dev->mode_config.fb_lock);
  496. }
  497. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  498. /**
  499. * drm_framebuffer_remove - remove and unreference a framebuffer object
  500. * @fb: framebuffer to remove
  501. *
  502. * Scans all the CRTCs and planes in @dev's mode_config. If they're
  503. * using @fb, removes it, setting it to NULL. Then drops the reference to the
  504. * passed-in framebuffer. Might take the modeset locks.
  505. *
  506. * Note that this function optimizes the cleanup away if the caller holds the
  507. * last reference to the framebuffer. It is also guaranteed to not take the
  508. * modeset locks in this case.
  509. */
  510. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  511. {
  512. struct drm_device *dev;
  513. struct drm_crtc *crtc;
  514. struct drm_plane *plane;
  515. struct drm_mode_set set;
  516. int ret;
  517. if (!fb)
  518. return;
  519. dev = fb->dev;
  520. WARN_ON(!list_empty(&fb->filp_head));
  521. /*
  522. * drm ABI mandates that we remove any deleted framebuffers from active
  523. * useage. But since most sane clients only remove framebuffers they no
  524. * longer need, try to optimize this away.
  525. *
  526. * Since we're holding a reference ourselves, observing a refcount of 1
  527. * means that we're the last holder and can skip it. Also, the refcount
  528. * can never increase from 1 again, so we don't need any barriers or
  529. * locks.
  530. *
  531. * Note that userspace could try to race with use and instate a new
  532. * usage _after_ we've cleared all current ones. End result will be an
  533. * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  534. * in this manner.
  535. */
  536. if (drm_framebuffer_read_refcount(fb) > 1) {
  537. drm_modeset_lock_all(dev);
  538. /* remove from any CRTC */
  539. drm_for_each_crtc(crtc, dev) {
  540. if (crtc->primary->fb == fb) {
  541. /* should turn off the crtc */
  542. memset(&set, 0, sizeof(struct drm_mode_set));
  543. set.crtc = crtc;
  544. set.fb = NULL;
  545. ret = drm_mode_set_config_internal(&set);
  546. if (ret)
  547. DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  548. }
  549. }
  550. drm_for_each_plane(plane, dev) {
  551. if (plane->fb == fb)
  552. drm_plane_force_disable(plane);
  553. }
  554. drm_modeset_unlock_all(dev);
  555. }
  556. drm_framebuffer_unreference(fb);
  557. }
  558. EXPORT_SYMBOL(drm_framebuffer_remove);
  559. DEFINE_WW_CLASS(crtc_ww_class);
  560. static unsigned int drm_num_crtcs(struct drm_device *dev)
  561. {
  562. unsigned int num = 0;
  563. struct drm_crtc *tmp;
  564. drm_for_each_crtc(tmp, dev) {
  565. num++;
  566. }
  567. return num;
  568. }
  569. /**
  570. * drm_crtc_init_with_planes - Initialise a new CRTC object with
  571. * specified primary and cursor planes.
  572. * @dev: DRM device
  573. * @crtc: CRTC object to init
  574. * @primary: Primary plane for CRTC
  575. * @cursor: Cursor plane for CRTC
  576. * @funcs: callbacks for the new CRTC
  577. * @name: printf style format string for the CRTC name, or NULL for default name
  578. *
  579. * Inits a new object created as base part of a driver crtc object.
  580. *
  581. * Returns:
  582. * Zero on success, error code on failure.
  583. */
  584. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  585. struct drm_plane *primary,
  586. struct drm_plane *cursor,
  587. const struct drm_crtc_funcs *funcs,
  588. const char *name, ...)
  589. {
  590. struct drm_mode_config *config = &dev->mode_config;
  591. int ret;
  592. WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
  593. WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
  594. crtc->dev = dev;
  595. crtc->funcs = funcs;
  596. drm_modeset_lock_init(&crtc->mutex);
  597. ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  598. if (ret)
  599. return ret;
  600. if (name) {
  601. va_list ap;
  602. va_start(ap, name);
  603. crtc->name = kvasprintf(GFP_KERNEL, name, ap);
  604. va_end(ap);
  605. } else {
  606. crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
  607. drm_num_crtcs(dev));
  608. }
  609. if (!crtc->name) {
  610. drm_mode_object_unregister(dev, &crtc->base);
  611. return -ENOMEM;
  612. }
  613. crtc->base.properties = &crtc->properties;
  614. list_add_tail(&crtc->head, &config->crtc_list);
  615. config->num_crtc++;
  616. crtc->primary = primary;
  617. crtc->cursor = cursor;
  618. if (primary)
  619. primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  620. if (cursor)
  621. cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  622. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  623. drm_object_attach_property(&crtc->base, config->prop_active, 0);
  624. drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
  625. }
  626. return 0;
  627. }
  628. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  629. /**
  630. * drm_crtc_cleanup - Clean up the core crtc usage
  631. * @crtc: CRTC to cleanup
  632. *
  633. * This function cleans up @crtc and removes it from the DRM mode setting
  634. * core. Note that the function does *not* free the crtc structure itself,
  635. * this is the responsibility of the caller.
  636. */
  637. void drm_crtc_cleanup(struct drm_crtc *crtc)
  638. {
  639. struct drm_device *dev = crtc->dev;
  640. kfree(crtc->gamma_store);
  641. crtc->gamma_store = NULL;
  642. drm_modeset_lock_fini(&crtc->mutex);
  643. drm_mode_object_unregister(dev, &crtc->base);
  644. list_del(&crtc->head);
  645. dev->mode_config.num_crtc--;
  646. WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  647. if (crtc->state && crtc->funcs->atomic_destroy_state)
  648. crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  649. kfree(crtc->name);
  650. memset(crtc, 0, sizeof(*crtc));
  651. }
  652. EXPORT_SYMBOL(drm_crtc_cleanup);
  653. /**
  654. * drm_crtc_index - find the index of a registered CRTC
  655. * @crtc: CRTC to find index for
  656. *
  657. * Given a registered CRTC, return the index of that CRTC within a DRM
  658. * device's list of CRTCs.
  659. */
  660. unsigned int drm_crtc_index(struct drm_crtc *crtc)
  661. {
  662. unsigned int index = 0;
  663. struct drm_crtc *tmp;
  664. drm_for_each_crtc(tmp, crtc->dev) {
  665. if (tmp == crtc)
  666. return index;
  667. index++;
  668. }
  669. BUG();
  670. }
  671. EXPORT_SYMBOL(drm_crtc_index);
  672. /*
  673. * drm_mode_remove - remove and free a mode
  674. * @connector: connector list to modify
  675. * @mode: mode to remove
  676. *
  677. * Remove @mode from @connector's mode list, then free it.
  678. */
  679. static void drm_mode_remove(struct drm_connector *connector,
  680. struct drm_display_mode *mode)
  681. {
  682. list_del(&mode->head);
  683. drm_mode_destroy(connector->dev, mode);
  684. }
  685. /**
  686. * drm_display_info_set_bus_formats - set the supported bus formats
  687. * @info: display info to store bus formats in
  688. * @formats: array containing the supported bus formats
  689. * @num_formats: the number of entries in the fmts array
  690. *
  691. * Store the supported bus formats in display info structure.
  692. * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
  693. * a full list of available formats.
  694. */
  695. int drm_display_info_set_bus_formats(struct drm_display_info *info,
  696. const u32 *formats,
  697. unsigned int num_formats)
  698. {
  699. u32 *fmts = NULL;
  700. if (!formats && num_formats)
  701. return -EINVAL;
  702. if (formats && num_formats) {
  703. fmts = kmemdup(formats, sizeof(*formats) * num_formats,
  704. GFP_KERNEL);
  705. if (!fmts)
  706. return -ENOMEM;
  707. }
  708. kfree(info->bus_formats);
  709. info->bus_formats = fmts;
  710. info->num_bus_formats = num_formats;
  711. return 0;
  712. }
  713. EXPORT_SYMBOL(drm_display_info_set_bus_formats);
  714. /**
  715. * drm_connector_get_cmdline_mode - reads the user's cmdline mode
  716. * @connector: connector to quwery
  717. *
  718. * The kernel supports per-connector configration of its consoles through
  719. * use of the video= parameter. This function parses that option and
  720. * extracts the user's specified mode (or enable/disable status) for a
  721. * particular connector. This is typically only used during the early fbdev
  722. * setup.
  723. */
  724. static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
  725. {
  726. struct drm_cmdline_mode *mode = &connector->cmdline_mode;
  727. char *option = NULL;
  728. if (fb_get_options(connector->name, &option))
  729. return;
  730. if (!drm_mode_parse_command_line_for_connector(option,
  731. connector,
  732. mode))
  733. return;
  734. if (mode->force) {
  735. const char *s;
  736. switch (mode->force) {
  737. case DRM_FORCE_OFF:
  738. s = "OFF";
  739. break;
  740. case DRM_FORCE_ON_DIGITAL:
  741. s = "ON - dig";
  742. break;
  743. default:
  744. case DRM_FORCE_ON:
  745. s = "ON";
  746. break;
  747. }
  748. DRM_INFO("forcing %s connector %s\n", connector->name, s);
  749. connector->force = mode->force;
  750. }
  751. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  752. connector->name,
  753. mode->xres, mode->yres,
  754. mode->refresh_specified ? mode->refresh : 60,
  755. mode->rb ? " reduced blanking" : "",
  756. mode->margins ? " with margins" : "",
  757. mode->interlace ? " interlaced" : "");
  758. }
  759. static void drm_connector_free(struct kref *kref)
  760. {
  761. struct drm_connector *connector =
  762. container_of(kref, struct drm_connector, base.refcount);
  763. struct drm_device *dev = connector->dev;
  764. drm_mode_object_unregister(dev, &connector->base);
  765. connector->funcs->destroy(connector);
  766. }
  767. /**
  768. * drm_connector_init - Init a preallocated connector
  769. * @dev: DRM device
  770. * @connector: the connector to init
  771. * @funcs: callbacks for this connector
  772. * @connector_type: user visible type of the connector
  773. *
  774. * Initialises a preallocated connector. Connectors should be
  775. * subclassed as part of driver connector objects.
  776. *
  777. * Returns:
  778. * Zero on success, error code on failure.
  779. */
  780. int drm_connector_init(struct drm_device *dev,
  781. struct drm_connector *connector,
  782. const struct drm_connector_funcs *funcs,
  783. int connector_type)
  784. {
  785. struct drm_mode_config *config = &dev->mode_config;
  786. int ret;
  787. struct ida *connector_ida =
  788. &drm_connector_enum_list[connector_type].ida;
  789. drm_modeset_lock_all(dev);
  790. ret = drm_mode_object_get_reg(dev, &connector->base,
  791. DRM_MODE_OBJECT_CONNECTOR,
  792. false, drm_connector_free);
  793. if (ret)
  794. goto out_unlock;
  795. connector->base.properties = &connector->properties;
  796. connector->dev = dev;
  797. connector->funcs = funcs;
  798. connector->connector_id = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
  799. if (connector->connector_id < 0) {
  800. ret = connector->connector_id;
  801. goto out_put;
  802. }
  803. connector->connector_type = connector_type;
  804. connector->connector_type_id =
  805. ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  806. if (connector->connector_type_id < 0) {
  807. ret = connector->connector_type_id;
  808. goto out_put_id;
  809. }
  810. connector->name =
  811. kasprintf(GFP_KERNEL, "%s-%d",
  812. drm_connector_enum_list[connector_type].name,
  813. connector->connector_type_id);
  814. if (!connector->name) {
  815. ret = -ENOMEM;
  816. goto out_put_type_id;
  817. }
  818. INIT_LIST_HEAD(&connector->probed_modes);
  819. INIT_LIST_HEAD(&connector->modes);
  820. connector->edid_blob_ptr = NULL;
  821. connector->status = connector_status_unknown;
  822. drm_connector_get_cmdline_mode(connector);
  823. /* We should add connectors at the end to avoid upsetting the connector
  824. * index too much. */
  825. list_add_tail(&connector->head, &config->connector_list);
  826. config->num_connector++;
  827. if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  828. drm_object_attach_property(&connector->base,
  829. config->edid_property,
  830. 0);
  831. drm_object_attach_property(&connector->base,
  832. config->dpms_property, 0);
  833. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  834. drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
  835. }
  836. connector->debugfs_entry = NULL;
  837. out_put_type_id:
  838. if (ret)
  839. ida_remove(connector_ida, connector->connector_type_id);
  840. out_put_id:
  841. if (ret)
  842. ida_remove(&config->connector_ida, connector->connector_id);
  843. out_put:
  844. if (ret)
  845. drm_mode_object_unregister(dev, &connector->base);
  846. out_unlock:
  847. drm_modeset_unlock_all(dev);
  848. return ret;
  849. }
  850. EXPORT_SYMBOL(drm_connector_init);
  851. /**
  852. * drm_connector_cleanup - cleans up an initialised connector
  853. * @connector: connector to cleanup
  854. *
  855. * Cleans up the connector but doesn't free the object.
  856. */
  857. void drm_connector_cleanup(struct drm_connector *connector)
  858. {
  859. struct drm_device *dev = connector->dev;
  860. struct drm_display_mode *mode, *t;
  861. if (connector->tile_group) {
  862. drm_mode_put_tile_group(dev, connector->tile_group);
  863. connector->tile_group = NULL;
  864. }
  865. list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  866. drm_mode_remove(connector, mode);
  867. list_for_each_entry_safe(mode, t, &connector->modes, head)
  868. drm_mode_remove(connector, mode);
  869. ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  870. connector->connector_type_id);
  871. ida_remove(&dev->mode_config.connector_ida,
  872. connector->connector_id);
  873. kfree(connector->display_info.bus_formats);
  874. drm_mode_object_unregister(dev, &connector->base);
  875. kfree(connector->name);
  876. connector->name = NULL;
  877. list_del(&connector->head);
  878. dev->mode_config.num_connector--;
  879. WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  880. if (connector->state && connector->funcs->atomic_destroy_state)
  881. connector->funcs->atomic_destroy_state(connector,
  882. connector->state);
  883. memset(connector, 0, sizeof(*connector));
  884. }
  885. EXPORT_SYMBOL(drm_connector_cleanup);
  886. /**
  887. * drm_connector_register - register a connector
  888. * @connector: the connector to register
  889. *
  890. * Register userspace interfaces for a connector
  891. *
  892. * Returns:
  893. * Zero on success, error code on failure.
  894. */
  895. int drm_connector_register(struct drm_connector *connector)
  896. {
  897. int ret;
  898. ret = drm_sysfs_connector_add(connector);
  899. if (ret)
  900. return ret;
  901. ret = drm_debugfs_connector_add(connector);
  902. if (ret) {
  903. drm_sysfs_connector_remove(connector);
  904. return ret;
  905. }
  906. drm_mode_object_register(connector->dev, &connector->base);
  907. return 0;
  908. }
  909. EXPORT_SYMBOL(drm_connector_register);
  910. /**
  911. * drm_connector_unregister - unregister a connector
  912. * @connector: the connector to unregister
  913. *
  914. * Unregister userspace interfaces for a connector
  915. */
  916. void drm_connector_unregister(struct drm_connector *connector)
  917. {
  918. drm_sysfs_connector_remove(connector);
  919. drm_debugfs_connector_remove(connector);
  920. }
  921. EXPORT_SYMBOL(drm_connector_unregister);
  922. /**
  923. * drm_connector_register_all - register all connectors
  924. * @dev: drm device
  925. *
  926. * This function registers all connectors in sysfs and other places so that
  927. * userspace can start to access them. Drivers can call it after calling
  928. * drm_dev_register() to complete the device registration, if they don't call
  929. * drm_connector_register() on each connector individually.
  930. *
  931. * When a device is unplugged and should be removed from userspace access,
  932. * call drm_connector_unregister_all(), which is the inverse of this
  933. * function.
  934. *
  935. * Returns:
  936. * Zero on success, error code on failure.
  937. */
  938. int drm_connector_register_all(struct drm_device *dev)
  939. {
  940. struct drm_connector *connector;
  941. int ret;
  942. mutex_lock(&dev->mode_config.mutex);
  943. drm_for_each_connector(connector, dev) {
  944. ret = drm_connector_register(connector);
  945. if (ret)
  946. goto err;
  947. }
  948. mutex_unlock(&dev->mode_config.mutex);
  949. return 0;
  950. err:
  951. mutex_unlock(&dev->mode_config.mutex);
  952. drm_connector_unregister_all(dev);
  953. return ret;
  954. }
  955. EXPORT_SYMBOL(drm_connector_register_all);
  956. /**
  957. * drm_connector_unregister_all - unregister connector userspace interfaces
  958. * @dev: drm device
  959. *
  960. * This functions unregisters all connectors from sysfs and other places so
  961. * that userspace can no longer access them. Drivers should call this as the
  962. * first step tearing down the device instace, or when the underlying
  963. * physical device disappeared (e.g. USB unplug), right before calling
  964. * drm_dev_unregister().
  965. */
  966. void drm_connector_unregister_all(struct drm_device *dev)
  967. {
  968. struct drm_connector *connector;
  969. /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
  970. list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  971. drm_connector_unregister(connector);
  972. }
  973. EXPORT_SYMBOL(drm_connector_unregister_all);
  974. /**
  975. * drm_encoder_init - Init a preallocated encoder
  976. * @dev: drm device
  977. * @encoder: the encoder to init
  978. * @funcs: callbacks for this encoder
  979. * @encoder_type: user visible type of the encoder
  980. * @name: printf style format string for the encoder name, or NULL for default name
  981. *
  982. * Initialises a preallocated encoder. Encoder should be
  983. * subclassed as part of driver encoder objects.
  984. *
  985. * Returns:
  986. * Zero on success, error code on failure.
  987. */
  988. int drm_encoder_init(struct drm_device *dev,
  989. struct drm_encoder *encoder,
  990. const struct drm_encoder_funcs *funcs,
  991. int encoder_type, const char *name, ...)
  992. {
  993. int ret;
  994. drm_modeset_lock_all(dev);
  995. ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  996. if (ret)
  997. goto out_unlock;
  998. encoder->dev = dev;
  999. encoder->encoder_type = encoder_type;
  1000. encoder->funcs = funcs;
  1001. if (name) {
  1002. va_list ap;
  1003. va_start(ap, name);
  1004. encoder->name = kvasprintf(GFP_KERNEL, name, ap);
  1005. va_end(ap);
  1006. } else {
  1007. encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  1008. drm_encoder_enum_list[encoder_type].name,
  1009. encoder->base.id);
  1010. }
  1011. if (!encoder->name) {
  1012. ret = -ENOMEM;
  1013. goto out_put;
  1014. }
  1015. list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  1016. dev->mode_config.num_encoder++;
  1017. out_put:
  1018. if (ret)
  1019. drm_mode_object_unregister(dev, &encoder->base);
  1020. out_unlock:
  1021. drm_modeset_unlock_all(dev);
  1022. return ret;
  1023. }
  1024. EXPORT_SYMBOL(drm_encoder_init);
  1025. /**
  1026. * drm_encoder_index - find the index of a registered encoder
  1027. * @encoder: encoder to find index for
  1028. *
  1029. * Given a registered encoder, return the index of that encoder within a DRM
  1030. * device's list of encoders.
  1031. */
  1032. unsigned int drm_encoder_index(struct drm_encoder *encoder)
  1033. {
  1034. unsigned int index = 0;
  1035. struct drm_encoder *tmp;
  1036. drm_for_each_encoder(tmp, encoder->dev) {
  1037. if (tmp == encoder)
  1038. return index;
  1039. index++;
  1040. }
  1041. BUG();
  1042. }
  1043. EXPORT_SYMBOL(drm_encoder_index);
  1044. /**
  1045. * drm_encoder_cleanup - cleans up an initialised encoder
  1046. * @encoder: encoder to cleanup
  1047. *
  1048. * Cleans up the encoder but doesn't free the object.
  1049. */
  1050. void drm_encoder_cleanup(struct drm_encoder *encoder)
  1051. {
  1052. struct drm_device *dev = encoder->dev;
  1053. drm_modeset_lock_all(dev);
  1054. drm_mode_object_unregister(dev, &encoder->base);
  1055. kfree(encoder->name);
  1056. list_del(&encoder->head);
  1057. dev->mode_config.num_encoder--;
  1058. drm_modeset_unlock_all(dev);
  1059. memset(encoder, 0, sizeof(*encoder));
  1060. }
  1061. EXPORT_SYMBOL(drm_encoder_cleanup);
  1062. static unsigned int drm_num_planes(struct drm_device *dev)
  1063. {
  1064. unsigned int num = 0;
  1065. struct drm_plane *tmp;
  1066. drm_for_each_plane(tmp, dev) {
  1067. num++;
  1068. }
  1069. return num;
  1070. }
  1071. /**
  1072. * drm_universal_plane_init - Initialize a new universal plane object
  1073. * @dev: DRM device
  1074. * @plane: plane object to init
  1075. * @possible_crtcs: bitmask of possible CRTCs
  1076. * @funcs: callbacks for the new plane
  1077. * @formats: array of supported formats (%DRM_FORMAT_*)
  1078. * @format_count: number of elements in @formats
  1079. * @type: type of plane (overlay, primary, cursor)
  1080. * @name: printf style format string for the plane name, or NULL for default name
  1081. *
  1082. * Initializes a plane object of type @type.
  1083. *
  1084. * Returns:
  1085. * Zero on success, error code on failure.
  1086. */
  1087. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1088. unsigned long possible_crtcs,
  1089. const struct drm_plane_funcs *funcs,
  1090. const uint32_t *formats, unsigned int format_count,
  1091. enum drm_plane_type type,
  1092. const char *name, ...)
  1093. {
  1094. struct drm_mode_config *config = &dev->mode_config;
  1095. int ret;
  1096. ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  1097. if (ret)
  1098. return ret;
  1099. drm_modeset_lock_init(&plane->mutex);
  1100. plane->base.properties = &plane->properties;
  1101. plane->dev = dev;
  1102. plane->funcs = funcs;
  1103. plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
  1104. GFP_KERNEL);
  1105. if (!plane->format_types) {
  1106. DRM_DEBUG_KMS("out of memory when allocating plane\n");
  1107. drm_mode_object_unregister(dev, &plane->base);
  1108. return -ENOMEM;
  1109. }
  1110. if (name) {
  1111. va_list ap;
  1112. va_start(ap, name);
  1113. plane->name = kvasprintf(GFP_KERNEL, name, ap);
  1114. va_end(ap);
  1115. } else {
  1116. plane->name = kasprintf(GFP_KERNEL, "plane-%d",
  1117. drm_num_planes(dev));
  1118. }
  1119. if (!plane->name) {
  1120. kfree(plane->format_types);
  1121. drm_mode_object_unregister(dev, &plane->base);
  1122. return -ENOMEM;
  1123. }
  1124. memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  1125. plane->format_count = format_count;
  1126. plane->possible_crtcs = possible_crtcs;
  1127. plane->type = type;
  1128. list_add_tail(&plane->head, &config->plane_list);
  1129. config->num_total_plane++;
  1130. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1131. config->num_overlay_plane++;
  1132. drm_object_attach_property(&plane->base,
  1133. config->plane_type_property,
  1134. plane->type);
  1135. if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
  1136. drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
  1137. drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
  1138. drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
  1139. drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
  1140. drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
  1141. drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
  1142. drm_object_attach_property(&plane->base, config->prop_src_x, 0);
  1143. drm_object_attach_property(&plane->base, config->prop_src_y, 0);
  1144. drm_object_attach_property(&plane->base, config->prop_src_w, 0);
  1145. drm_object_attach_property(&plane->base, config->prop_src_h, 0);
  1146. }
  1147. return 0;
  1148. }
  1149. EXPORT_SYMBOL(drm_universal_plane_init);
  1150. /**
  1151. * drm_plane_init - Initialize a legacy plane
  1152. * @dev: DRM device
  1153. * @plane: plane object to init
  1154. * @possible_crtcs: bitmask of possible CRTCs
  1155. * @funcs: callbacks for the new plane
  1156. * @formats: array of supported formats (%DRM_FORMAT_*)
  1157. * @format_count: number of elements in @formats
  1158. * @is_primary: plane type (primary vs overlay)
  1159. *
  1160. * Legacy API to initialize a DRM plane.
  1161. *
  1162. * New drivers should call drm_universal_plane_init() instead.
  1163. *
  1164. * Returns:
  1165. * Zero on success, error code on failure.
  1166. */
  1167. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1168. unsigned long possible_crtcs,
  1169. const struct drm_plane_funcs *funcs,
  1170. const uint32_t *formats, unsigned int format_count,
  1171. bool is_primary)
  1172. {
  1173. enum drm_plane_type type;
  1174. type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  1175. return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  1176. formats, format_count, type, NULL);
  1177. }
  1178. EXPORT_SYMBOL(drm_plane_init);
  1179. /**
  1180. * drm_plane_cleanup - Clean up the core plane usage
  1181. * @plane: plane to cleanup
  1182. *
  1183. * This function cleans up @plane and removes it from the DRM mode setting
  1184. * core. Note that the function does *not* free the plane structure itself,
  1185. * this is the responsibility of the caller.
  1186. */
  1187. void drm_plane_cleanup(struct drm_plane *plane)
  1188. {
  1189. struct drm_device *dev = plane->dev;
  1190. drm_modeset_lock_all(dev);
  1191. kfree(plane->format_types);
  1192. drm_mode_object_unregister(dev, &plane->base);
  1193. BUG_ON(list_empty(&plane->head));
  1194. list_del(&plane->head);
  1195. dev->mode_config.num_total_plane--;
  1196. if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1197. dev->mode_config.num_overlay_plane--;
  1198. drm_modeset_unlock_all(dev);
  1199. WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  1200. if (plane->state && plane->funcs->atomic_destroy_state)
  1201. plane->funcs->atomic_destroy_state(plane, plane->state);
  1202. kfree(plane->name);
  1203. memset(plane, 0, sizeof(*plane));
  1204. }
  1205. EXPORT_SYMBOL(drm_plane_cleanup);
  1206. /**
  1207. * drm_plane_index - find the index of a registered plane
  1208. * @plane: plane to find index for
  1209. *
  1210. * Given a registered plane, return the index of that CRTC within a DRM
  1211. * device's list of planes.
  1212. */
  1213. unsigned int drm_plane_index(struct drm_plane *plane)
  1214. {
  1215. unsigned int index = 0;
  1216. struct drm_plane *tmp;
  1217. drm_for_each_plane(tmp, plane->dev) {
  1218. if (tmp == plane)
  1219. return index;
  1220. index++;
  1221. }
  1222. BUG();
  1223. }
  1224. EXPORT_SYMBOL(drm_plane_index);
  1225. /**
  1226. * drm_plane_from_index - find the registered plane at an index
  1227. * @dev: DRM device
  1228. * @idx: index of registered plane to find for
  1229. *
  1230. * Given a plane index, return the registered plane from DRM device's
  1231. * list of planes with matching index.
  1232. */
  1233. struct drm_plane *
  1234. drm_plane_from_index(struct drm_device *dev, int idx)
  1235. {
  1236. struct drm_plane *plane;
  1237. unsigned int i = 0;
  1238. drm_for_each_plane(plane, dev) {
  1239. if (i == idx)
  1240. return plane;
  1241. i++;
  1242. }
  1243. return NULL;
  1244. }
  1245. EXPORT_SYMBOL(drm_plane_from_index);
  1246. /**
  1247. * drm_plane_force_disable - Forcibly disable a plane
  1248. * @plane: plane to disable
  1249. *
  1250. * Forces the plane to be disabled.
  1251. *
  1252. * Used when the plane's current framebuffer is destroyed,
  1253. * and when restoring fbdev mode.
  1254. */
  1255. void drm_plane_force_disable(struct drm_plane *plane)
  1256. {
  1257. int ret;
  1258. if (!plane->fb)
  1259. return;
  1260. plane->old_fb = plane->fb;
  1261. ret = plane->funcs->disable_plane(plane);
  1262. if (ret) {
  1263. DRM_ERROR("failed to disable plane with busy fb\n");
  1264. plane->old_fb = NULL;
  1265. return;
  1266. }
  1267. /* disconnect the plane from the fb and crtc: */
  1268. drm_framebuffer_unreference(plane->old_fb);
  1269. plane->old_fb = NULL;
  1270. plane->fb = NULL;
  1271. plane->crtc = NULL;
  1272. }
  1273. EXPORT_SYMBOL(drm_plane_force_disable);
  1274. static int drm_mode_create_standard_properties(struct drm_device *dev)
  1275. {
  1276. struct drm_property *prop;
  1277. /*
  1278. * Standard properties (apply to all connectors)
  1279. */
  1280. prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  1281. DRM_MODE_PROP_IMMUTABLE,
  1282. "EDID", 0);
  1283. if (!prop)
  1284. return -ENOMEM;
  1285. dev->mode_config.edid_property = prop;
  1286. prop = drm_property_create_enum(dev, 0,
  1287. "DPMS", drm_dpms_enum_list,
  1288. ARRAY_SIZE(drm_dpms_enum_list));
  1289. if (!prop)
  1290. return -ENOMEM;
  1291. dev->mode_config.dpms_property = prop;
  1292. prop = drm_property_create(dev,
  1293. DRM_MODE_PROP_BLOB |
  1294. DRM_MODE_PROP_IMMUTABLE,
  1295. "PATH", 0);
  1296. if (!prop)
  1297. return -ENOMEM;
  1298. dev->mode_config.path_property = prop;
  1299. prop = drm_property_create(dev,
  1300. DRM_MODE_PROP_BLOB |
  1301. DRM_MODE_PROP_IMMUTABLE,
  1302. "TILE", 0);
  1303. if (!prop)
  1304. return -ENOMEM;
  1305. dev->mode_config.tile_property = prop;
  1306. prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1307. "type", drm_plane_type_enum_list,
  1308. ARRAY_SIZE(drm_plane_type_enum_list));
  1309. if (!prop)
  1310. return -ENOMEM;
  1311. dev->mode_config.plane_type_property = prop;
  1312. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1313. "SRC_X", 0, UINT_MAX);
  1314. if (!prop)
  1315. return -ENOMEM;
  1316. dev->mode_config.prop_src_x = prop;
  1317. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1318. "SRC_Y", 0, UINT_MAX);
  1319. if (!prop)
  1320. return -ENOMEM;
  1321. dev->mode_config.prop_src_y = prop;
  1322. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1323. "SRC_W", 0, UINT_MAX);
  1324. if (!prop)
  1325. return -ENOMEM;
  1326. dev->mode_config.prop_src_w = prop;
  1327. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1328. "SRC_H", 0, UINT_MAX);
  1329. if (!prop)
  1330. return -ENOMEM;
  1331. dev->mode_config.prop_src_h = prop;
  1332. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1333. "CRTC_X", INT_MIN, INT_MAX);
  1334. if (!prop)
  1335. return -ENOMEM;
  1336. dev->mode_config.prop_crtc_x = prop;
  1337. prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
  1338. "CRTC_Y", INT_MIN, INT_MAX);
  1339. if (!prop)
  1340. return -ENOMEM;
  1341. dev->mode_config.prop_crtc_y = prop;
  1342. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1343. "CRTC_W", 0, INT_MAX);
  1344. if (!prop)
  1345. return -ENOMEM;
  1346. dev->mode_config.prop_crtc_w = prop;
  1347. prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
  1348. "CRTC_H", 0, INT_MAX);
  1349. if (!prop)
  1350. return -ENOMEM;
  1351. dev->mode_config.prop_crtc_h = prop;
  1352. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1353. "FB_ID", DRM_MODE_OBJECT_FB);
  1354. if (!prop)
  1355. return -ENOMEM;
  1356. dev->mode_config.prop_fb_id = prop;
  1357. prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
  1358. "CRTC_ID", DRM_MODE_OBJECT_CRTC);
  1359. if (!prop)
  1360. return -ENOMEM;
  1361. dev->mode_config.prop_crtc_id = prop;
  1362. prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
  1363. "ACTIVE");
  1364. if (!prop)
  1365. return -ENOMEM;
  1366. dev->mode_config.prop_active = prop;
  1367. prop = drm_property_create(dev,
  1368. DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
  1369. "MODE_ID", 0);
  1370. if (!prop)
  1371. return -ENOMEM;
  1372. dev->mode_config.prop_mode_id = prop;
  1373. prop = drm_property_create(dev,
  1374. DRM_MODE_PROP_BLOB,
  1375. "DEGAMMA_LUT", 0);
  1376. if (!prop)
  1377. return -ENOMEM;
  1378. dev->mode_config.degamma_lut_property = prop;
  1379. prop = drm_property_create_range(dev,
  1380. DRM_MODE_PROP_IMMUTABLE,
  1381. "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
  1382. if (!prop)
  1383. return -ENOMEM;
  1384. dev->mode_config.degamma_lut_size_property = prop;
  1385. prop = drm_property_create(dev,
  1386. DRM_MODE_PROP_BLOB,
  1387. "CTM", 0);
  1388. if (!prop)
  1389. return -ENOMEM;
  1390. dev->mode_config.ctm_property = prop;
  1391. prop = drm_property_create(dev,
  1392. DRM_MODE_PROP_BLOB,
  1393. "GAMMA_LUT", 0);
  1394. if (!prop)
  1395. return -ENOMEM;
  1396. dev->mode_config.gamma_lut_property = prop;
  1397. prop = drm_property_create_range(dev,
  1398. DRM_MODE_PROP_IMMUTABLE,
  1399. "GAMMA_LUT_SIZE", 0, UINT_MAX);
  1400. if (!prop)
  1401. return -ENOMEM;
  1402. dev->mode_config.gamma_lut_size_property = prop;
  1403. return 0;
  1404. }
  1405. /**
  1406. * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  1407. * @dev: DRM device
  1408. *
  1409. * Called by a driver the first time a DVI-I connector is made.
  1410. */
  1411. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  1412. {
  1413. struct drm_property *dvi_i_selector;
  1414. struct drm_property *dvi_i_subconnector;
  1415. if (dev->mode_config.dvi_i_select_subconnector_property)
  1416. return 0;
  1417. dvi_i_selector =
  1418. drm_property_create_enum(dev, 0,
  1419. "select subconnector",
  1420. drm_dvi_i_select_enum_list,
  1421. ARRAY_SIZE(drm_dvi_i_select_enum_list));
  1422. dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  1423. dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1424. "subconnector",
  1425. drm_dvi_i_subconnector_enum_list,
  1426. ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  1427. dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  1428. return 0;
  1429. }
  1430. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  1431. /**
  1432. * drm_create_tv_properties - create TV specific connector properties
  1433. * @dev: DRM device
  1434. * @num_modes: number of different TV formats (modes) supported
  1435. * @modes: array of pointers to strings containing name of each format
  1436. *
  1437. * Called by a driver's TV initialization routine, this function creates
  1438. * the TV specific connector properties for a given device. Caller is
  1439. * responsible for allocating a list of format names and passing them to
  1440. * this routine.
  1441. */
  1442. int drm_mode_create_tv_properties(struct drm_device *dev,
  1443. unsigned int num_modes,
  1444. const char * const modes[])
  1445. {
  1446. struct drm_property *tv_selector;
  1447. struct drm_property *tv_subconnector;
  1448. unsigned int i;
  1449. if (dev->mode_config.tv_select_subconnector_property)
  1450. return 0;
  1451. /*
  1452. * Basic connector properties
  1453. */
  1454. tv_selector = drm_property_create_enum(dev, 0,
  1455. "select subconnector",
  1456. drm_tv_select_enum_list,
  1457. ARRAY_SIZE(drm_tv_select_enum_list));
  1458. if (!tv_selector)
  1459. goto nomem;
  1460. dev->mode_config.tv_select_subconnector_property = tv_selector;
  1461. tv_subconnector =
  1462. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1463. "subconnector",
  1464. drm_tv_subconnector_enum_list,
  1465. ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1466. if (!tv_subconnector)
  1467. goto nomem;
  1468. dev->mode_config.tv_subconnector_property = tv_subconnector;
  1469. /*
  1470. * Other, TV specific properties: margins & TV modes.
  1471. */
  1472. dev->mode_config.tv_left_margin_property =
  1473. drm_property_create_range(dev, 0, "left margin", 0, 100);
  1474. if (!dev->mode_config.tv_left_margin_property)
  1475. goto nomem;
  1476. dev->mode_config.tv_right_margin_property =
  1477. drm_property_create_range(dev, 0, "right margin", 0, 100);
  1478. if (!dev->mode_config.tv_right_margin_property)
  1479. goto nomem;
  1480. dev->mode_config.tv_top_margin_property =
  1481. drm_property_create_range(dev, 0, "top margin", 0, 100);
  1482. if (!dev->mode_config.tv_top_margin_property)
  1483. goto nomem;
  1484. dev->mode_config.tv_bottom_margin_property =
  1485. drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1486. if (!dev->mode_config.tv_bottom_margin_property)
  1487. goto nomem;
  1488. dev->mode_config.tv_mode_property =
  1489. drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1490. "mode", num_modes);
  1491. if (!dev->mode_config.tv_mode_property)
  1492. goto nomem;
  1493. for (i = 0; i < num_modes; i++)
  1494. drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  1495. i, modes[i]);
  1496. dev->mode_config.tv_brightness_property =
  1497. drm_property_create_range(dev, 0, "brightness", 0, 100);
  1498. if (!dev->mode_config.tv_brightness_property)
  1499. goto nomem;
  1500. dev->mode_config.tv_contrast_property =
  1501. drm_property_create_range(dev, 0, "contrast", 0, 100);
  1502. if (!dev->mode_config.tv_contrast_property)
  1503. goto nomem;
  1504. dev->mode_config.tv_flicker_reduction_property =
  1505. drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1506. if (!dev->mode_config.tv_flicker_reduction_property)
  1507. goto nomem;
  1508. dev->mode_config.tv_overscan_property =
  1509. drm_property_create_range(dev, 0, "overscan", 0, 100);
  1510. if (!dev->mode_config.tv_overscan_property)
  1511. goto nomem;
  1512. dev->mode_config.tv_saturation_property =
  1513. drm_property_create_range(dev, 0, "saturation", 0, 100);
  1514. if (!dev->mode_config.tv_saturation_property)
  1515. goto nomem;
  1516. dev->mode_config.tv_hue_property =
  1517. drm_property_create_range(dev, 0, "hue", 0, 100);
  1518. if (!dev->mode_config.tv_hue_property)
  1519. goto nomem;
  1520. return 0;
  1521. nomem:
  1522. return -ENOMEM;
  1523. }
  1524. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1525. /**
  1526. * drm_mode_create_scaling_mode_property - create scaling mode property
  1527. * @dev: DRM device
  1528. *
  1529. * Called by a driver the first time it's needed, must be attached to desired
  1530. * connectors.
  1531. */
  1532. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1533. {
  1534. struct drm_property *scaling_mode;
  1535. if (dev->mode_config.scaling_mode_property)
  1536. return 0;
  1537. scaling_mode =
  1538. drm_property_create_enum(dev, 0, "scaling mode",
  1539. drm_scaling_mode_enum_list,
  1540. ARRAY_SIZE(drm_scaling_mode_enum_list));
  1541. dev->mode_config.scaling_mode_property = scaling_mode;
  1542. return 0;
  1543. }
  1544. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1545. /**
  1546. * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1547. * @dev: DRM device
  1548. *
  1549. * Called by a driver the first time it's needed, must be attached to desired
  1550. * connectors.
  1551. *
  1552. * Returns:
  1553. * Zero on success, negative errno on failure.
  1554. */
  1555. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1556. {
  1557. if (dev->mode_config.aspect_ratio_property)
  1558. return 0;
  1559. dev->mode_config.aspect_ratio_property =
  1560. drm_property_create_enum(dev, 0, "aspect ratio",
  1561. drm_aspect_ratio_enum_list,
  1562. ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1563. if (dev->mode_config.aspect_ratio_property == NULL)
  1564. return -ENOMEM;
  1565. return 0;
  1566. }
  1567. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1568. /**
  1569. * drm_mode_create_dirty_property - create dirty property
  1570. * @dev: DRM device
  1571. *
  1572. * Called by a driver the first time it's needed, must be attached to desired
  1573. * connectors.
  1574. */
  1575. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1576. {
  1577. struct drm_property *dirty_info;
  1578. if (dev->mode_config.dirty_info_property)
  1579. return 0;
  1580. dirty_info =
  1581. drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1582. "dirty",
  1583. drm_dirty_info_enum_list,
  1584. ARRAY_SIZE(drm_dirty_info_enum_list));
  1585. dev->mode_config.dirty_info_property = dirty_info;
  1586. return 0;
  1587. }
  1588. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1589. /**
  1590. * drm_mode_create_suggested_offset_properties - create suggests offset properties
  1591. * @dev: DRM device
  1592. *
  1593. * Create the the suggested x/y offset property for connectors.
  1594. */
  1595. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  1596. {
  1597. if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  1598. return 0;
  1599. dev->mode_config.suggested_x_property =
  1600. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  1601. dev->mode_config.suggested_y_property =
  1602. drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  1603. if (dev->mode_config.suggested_x_property == NULL ||
  1604. dev->mode_config.suggested_y_property == NULL)
  1605. return -ENOMEM;
  1606. return 0;
  1607. }
  1608. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  1609. /**
  1610. * drm_mode_getresources - get graphics configuration
  1611. * @dev: drm device for the ioctl
  1612. * @data: data pointer for the ioctl
  1613. * @file_priv: drm file for the ioctl call
  1614. *
  1615. * Construct a set of configuration description structures and return
  1616. * them to the user, including CRTC, connector and framebuffer configuration.
  1617. *
  1618. * Called by the user via ioctl.
  1619. *
  1620. * Returns:
  1621. * Zero on success, negative errno on failure.
  1622. */
  1623. int drm_mode_getresources(struct drm_device *dev, void *data,
  1624. struct drm_file *file_priv)
  1625. {
  1626. struct drm_mode_card_res *card_res = data;
  1627. struct list_head *lh;
  1628. struct drm_framebuffer *fb;
  1629. struct drm_connector *connector;
  1630. struct drm_crtc *crtc;
  1631. struct drm_encoder *encoder;
  1632. int ret = 0;
  1633. int connector_count = 0;
  1634. int crtc_count = 0;
  1635. int fb_count = 0;
  1636. int encoder_count = 0;
  1637. int copied = 0;
  1638. uint32_t __user *fb_id;
  1639. uint32_t __user *crtc_id;
  1640. uint32_t __user *connector_id;
  1641. uint32_t __user *encoder_id;
  1642. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1643. return -EINVAL;
  1644. mutex_lock(&file_priv->fbs_lock);
  1645. /*
  1646. * For the non-control nodes we need to limit the list of resources
  1647. * by IDs in the group list for this node
  1648. */
  1649. list_for_each(lh, &file_priv->fbs)
  1650. fb_count++;
  1651. /* handle this in 4 parts */
  1652. /* FBs */
  1653. if (card_res->count_fbs >= fb_count) {
  1654. copied = 0;
  1655. fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1656. list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1657. if (put_user(fb->base.id, fb_id + copied)) {
  1658. mutex_unlock(&file_priv->fbs_lock);
  1659. return -EFAULT;
  1660. }
  1661. copied++;
  1662. }
  1663. }
  1664. card_res->count_fbs = fb_count;
  1665. mutex_unlock(&file_priv->fbs_lock);
  1666. /* mode_config.mutex protects the connector list against e.g. DP MST
  1667. * connector hot-adding. CRTC/Plane lists are invariant. */
  1668. mutex_lock(&dev->mode_config.mutex);
  1669. drm_for_each_crtc(crtc, dev)
  1670. crtc_count++;
  1671. drm_for_each_connector(connector, dev)
  1672. connector_count++;
  1673. drm_for_each_encoder(encoder, dev)
  1674. encoder_count++;
  1675. card_res->max_height = dev->mode_config.max_height;
  1676. card_res->min_height = dev->mode_config.min_height;
  1677. card_res->max_width = dev->mode_config.max_width;
  1678. card_res->min_width = dev->mode_config.min_width;
  1679. /* CRTCs */
  1680. if (card_res->count_crtcs >= crtc_count) {
  1681. copied = 0;
  1682. crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1683. drm_for_each_crtc(crtc, dev) {
  1684. if (put_user(crtc->base.id, crtc_id + copied)) {
  1685. ret = -EFAULT;
  1686. goto out;
  1687. }
  1688. copied++;
  1689. }
  1690. }
  1691. card_res->count_crtcs = crtc_count;
  1692. /* Encoders */
  1693. if (card_res->count_encoders >= encoder_count) {
  1694. copied = 0;
  1695. encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1696. drm_for_each_encoder(encoder, dev) {
  1697. if (put_user(encoder->base.id, encoder_id +
  1698. copied)) {
  1699. ret = -EFAULT;
  1700. goto out;
  1701. }
  1702. copied++;
  1703. }
  1704. }
  1705. card_res->count_encoders = encoder_count;
  1706. /* Connectors */
  1707. if (card_res->count_connectors >= connector_count) {
  1708. copied = 0;
  1709. connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1710. drm_for_each_connector(connector, dev) {
  1711. if (put_user(connector->base.id,
  1712. connector_id + copied)) {
  1713. ret = -EFAULT;
  1714. goto out;
  1715. }
  1716. copied++;
  1717. }
  1718. }
  1719. card_res->count_connectors = connector_count;
  1720. out:
  1721. mutex_unlock(&dev->mode_config.mutex);
  1722. return ret;
  1723. }
  1724. /**
  1725. * drm_mode_getcrtc - get CRTC configuration
  1726. * @dev: drm device for the ioctl
  1727. * @data: data pointer for the ioctl
  1728. * @file_priv: drm file for the ioctl call
  1729. *
  1730. * Construct a CRTC configuration structure to return to the user.
  1731. *
  1732. * Called by the user via ioctl.
  1733. *
  1734. * Returns:
  1735. * Zero on success, negative errno on failure.
  1736. */
  1737. int drm_mode_getcrtc(struct drm_device *dev,
  1738. void *data, struct drm_file *file_priv)
  1739. {
  1740. struct drm_mode_crtc *crtc_resp = data;
  1741. struct drm_crtc *crtc;
  1742. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1743. return -EINVAL;
  1744. crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1745. if (!crtc)
  1746. return -ENOENT;
  1747. drm_modeset_lock_crtc(crtc, crtc->primary);
  1748. crtc_resp->gamma_size = crtc->gamma_size;
  1749. if (crtc->primary->fb)
  1750. crtc_resp->fb_id = crtc->primary->fb->base.id;
  1751. else
  1752. crtc_resp->fb_id = 0;
  1753. if (crtc->state) {
  1754. crtc_resp->x = crtc->primary->state->src_x >> 16;
  1755. crtc_resp->y = crtc->primary->state->src_y >> 16;
  1756. if (crtc->state->enable) {
  1757. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
  1758. crtc_resp->mode_valid = 1;
  1759. } else {
  1760. crtc_resp->mode_valid = 0;
  1761. }
  1762. } else {
  1763. crtc_resp->x = crtc->x;
  1764. crtc_resp->y = crtc->y;
  1765. if (crtc->enabled) {
  1766. drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1767. crtc_resp->mode_valid = 1;
  1768. } else {
  1769. crtc_resp->mode_valid = 0;
  1770. }
  1771. }
  1772. drm_modeset_unlock_crtc(crtc);
  1773. return 0;
  1774. }
  1775. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1776. const struct drm_file *file_priv)
  1777. {
  1778. /*
  1779. * If user-space hasn't configured the driver to expose the stereo 3D
  1780. * modes, don't expose them.
  1781. */
  1782. if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1783. return false;
  1784. return true;
  1785. }
  1786. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1787. {
  1788. /* For atomic drivers only state objects are synchronously updated and
  1789. * protected by modeset locks, so check those first. */
  1790. if (connector->state)
  1791. return connector->state->best_encoder;
  1792. return connector->encoder;
  1793. }
  1794. /* helper for getconnector and getproperties ioctls */
  1795. static int get_properties(struct drm_mode_object *obj, bool atomic,
  1796. uint32_t __user *prop_ptr, uint64_t __user *prop_values,
  1797. uint32_t *arg_count_props)
  1798. {
  1799. int props_count;
  1800. int i, ret, copied;
  1801. props_count = obj->properties->count;
  1802. if (!atomic)
  1803. props_count -= obj->properties->atomic_count;
  1804. if ((*arg_count_props >= props_count) && props_count) {
  1805. for (i = 0, copied = 0; copied < props_count; i++) {
  1806. struct drm_property *prop = obj->properties->properties[i];
  1807. uint64_t val;
  1808. if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
  1809. continue;
  1810. ret = drm_object_property_get_value(obj, prop, &val);
  1811. if (ret)
  1812. return ret;
  1813. if (put_user(prop->base.id, prop_ptr + copied))
  1814. return -EFAULT;
  1815. if (put_user(val, prop_values + copied))
  1816. return -EFAULT;
  1817. copied++;
  1818. }
  1819. }
  1820. *arg_count_props = props_count;
  1821. return 0;
  1822. }
  1823. /**
  1824. * drm_mode_getconnector - get connector configuration
  1825. * @dev: drm device for the ioctl
  1826. * @data: data pointer for the ioctl
  1827. * @file_priv: drm file for the ioctl call
  1828. *
  1829. * Construct a connector configuration structure to return to the user.
  1830. *
  1831. * Called by the user via ioctl.
  1832. *
  1833. * Returns:
  1834. * Zero on success, negative errno on failure.
  1835. */
  1836. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1837. struct drm_file *file_priv)
  1838. {
  1839. struct drm_mode_get_connector *out_resp = data;
  1840. struct drm_connector *connector;
  1841. struct drm_encoder *encoder;
  1842. struct drm_display_mode *mode;
  1843. int mode_count = 0;
  1844. int encoders_count = 0;
  1845. int ret = 0;
  1846. int copied = 0;
  1847. int i;
  1848. struct drm_mode_modeinfo u_mode;
  1849. struct drm_mode_modeinfo __user *mode_ptr;
  1850. uint32_t __user *encoder_ptr;
  1851. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1852. return -EINVAL;
  1853. memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1854. mutex_lock(&dev->mode_config.mutex);
  1855. connector = drm_connector_lookup(dev, out_resp->connector_id);
  1856. if (!connector) {
  1857. ret = -ENOENT;
  1858. goto out_unlock;
  1859. }
  1860. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
  1861. if (connector->encoder_ids[i] != 0)
  1862. encoders_count++;
  1863. if (out_resp->count_modes == 0) {
  1864. connector->funcs->fill_modes(connector,
  1865. dev->mode_config.max_width,
  1866. dev->mode_config.max_height);
  1867. }
  1868. /* delayed so we get modes regardless of pre-fill_modes state */
  1869. list_for_each_entry(mode, &connector->modes, head)
  1870. if (drm_mode_expose_to_userspace(mode, file_priv))
  1871. mode_count++;
  1872. out_resp->connector_id = connector->base.id;
  1873. out_resp->connector_type = connector->connector_type;
  1874. out_resp->connector_type_id = connector->connector_type_id;
  1875. out_resp->mm_width = connector->display_info.width_mm;
  1876. out_resp->mm_height = connector->display_info.height_mm;
  1877. out_resp->subpixel = connector->display_info.subpixel_order;
  1878. out_resp->connection = connector->status;
  1879. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1880. encoder = drm_connector_get_encoder(connector);
  1881. if (encoder)
  1882. out_resp->encoder_id = encoder->base.id;
  1883. else
  1884. out_resp->encoder_id = 0;
  1885. /*
  1886. * This ioctl is called twice, once to determine how much space is
  1887. * needed, and the 2nd time to fill it.
  1888. */
  1889. if ((out_resp->count_modes >= mode_count) && mode_count) {
  1890. copied = 0;
  1891. mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1892. list_for_each_entry(mode, &connector->modes, head) {
  1893. if (!drm_mode_expose_to_userspace(mode, file_priv))
  1894. continue;
  1895. drm_mode_convert_to_umode(&u_mode, mode);
  1896. if (copy_to_user(mode_ptr + copied,
  1897. &u_mode, sizeof(u_mode))) {
  1898. ret = -EFAULT;
  1899. goto out;
  1900. }
  1901. copied++;
  1902. }
  1903. }
  1904. out_resp->count_modes = mode_count;
  1905. ret = get_properties(&connector->base, file_priv->atomic,
  1906. (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
  1907. (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
  1908. &out_resp->count_props);
  1909. if (ret)
  1910. goto out;
  1911. if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1912. copied = 0;
  1913. encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  1914. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1915. if (connector->encoder_ids[i] != 0) {
  1916. if (put_user(connector->encoder_ids[i],
  1917. encoder_ptr + copied)) {
  1918. ret = -EFAULT;
  1919. goto out;
  1920. }
  1921. copied++;
  1922. }
  1923. }
  1924. }
  1925. out_resp->count_encoders = encoders_count;
  1926. out:
  1927. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1928. drm_connector_unreference(connector);
  1929. out_unlock:
  1930. mutex_unlock(&dev->mode_config.mutex);
  1931. return ret;
  1932. }
  1933. static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
  1934. {
  1935. struct drm_connector *connector;
  1936. struct drm_device *dev = encoder->dev;
  1937. bool uses_atomic = false;
  1938. /* For atomic drivers only state objects are synchronously updated and
  1939. * protected by modeset locks, so check those first. */
  1940. drm_for_each_connector(connector, dev) {
  1941. if (!connector->state)
  1942. continue;
  1943. uses_atomic = true;
  1944. if (connector->state->best_encoder != encoder)
  1945. continue;
  1946. return connector->state->crtc;
  1947. }
  1948. /* Don't return stale data (e.g. pending async disable). */
  1949. if (uses_atomic)
  1950. return NULL;
  1951. return encoder->crtc;
  1952. }
  1953. /**
  1954. * drm_mode_getencoder - get encoder configuration
  1955. * @dev: drm device for the ioctl
  1956. * @data: data pointer for the ioctl
  1957. * @file_priv: drm file for the ioctl call
  1958. *
  1959. * Construct a encoder configuration structure to return to the user.
  1960. *
  1961. * Called by the user via ioctl.
  1962. *
  1963. * Returns:
  1964. * Zero on success, negative errno on failure.
  1965. */
  1966. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1967. struct drm_file *file_priv)
  1968. {
  1969. struct drm_mode_get_encoder *enc_resp = data;
  1970. struct drm_encoder *encoder;
  1971. struct drm_crtc *crtc;
  1972. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1973. return -EINVAL;
  1974. encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  1975. if (!encoder)
  1976. return -ENOENT;
  1977. drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1978. crtc = drm_encoder_get_crtc(encoder);
  1979. if (crtc)
  1980. enc_resp->crtc_id = crtc->base.id;
  1981. else
  1982. enc_resp->crtc_id = 0;
  1983. drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1984. enc_resp->encoder_type = encoder->encoder_type;
  1985. enc_resp->encoder_id = encoder->base.id;
  1986. enc_resp->possible_crtcs = encoder->possible_crtcs;
  1987. enc_resp->possible_clones = encoder->possible_clones;
  1988. return 0;
  1989. }
  1990. /**
  1991. * drm_mode_getplane_res - enumerate all plane resources
  1992. * @dev: DRM device
  1993. * @data: ioctl data
  1994. * @file_priv: DRM file info
  1995. *
  1996. * Construct a list of plane ids to return to the user.
  1997. *
  1998. * Called by the user via ioctl.
  1999. *
  2000. * Returns:
  2001. * Zero on success, negative errno on failure.
  2002. */
  2003. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  2004. struct drm_file *file_priv)
  2005. {
  2006. struct drm_mode_get_plane_res *plane_resp = data;
  2007. struct drm_mode_config *config;
  2008. struct drm_plane *plane;
  2009. uint32_t __user *plane_ptr;
  2010. int copied = 0;
  2011. unsigned num_planes;
  2012. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2013. return -EINVAL;
  2014. config = &dev->mode_config;
  2015. if (file_priv->universal_planes)
  2016. num_planes = config->num_total_plane;
  2017. else
  2018. num_planes = config->num_overlay_plane;
  2019. /*
  2020. * This ioctl is called twice, once to determine how much space is
  2021. * needed, and the 2nd time to fill it.
  2022. */
  2023. if (num_planes &&
  2024. (plane_resp->count_planes >= num_planes)) {
  2025. plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  2026. /* Plane lists are invariant, no locking needed. */
  2027. drm_for_each_plane(plane, dev) {
  2028. /*
  2029. * Unless userspace set the 'universal planes'
  2030. * capability bit, only advertise overlays.
  2031. */
  2032. if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  2033. !file_priv->universal_planes)
  2034. continue;
  2035. if (put_user(plane->base.id, plane_ptr + copied))
  2036. return -EFAULT;
  2037. copied++;
  2038. }
  2039. }
  2040. plane_resp->count_planes = num_planes;
  2041. return 0;
  2042. }
  2043. /**
  2044. * drm_mode_getplane - get plane configuration
  2045. * @dev: DRM device
  2046. * @data: ioctl data
  2047. * @file_priv: DRM file info
  2048. *
  2049. * Construct a plane configuration structure to return to the user.
  2050. *
  2051. * Called by the user via ioctl.
  2052. *
  2053. * Returns:
  2054. * Zero on success, negative errno on failure.
  2055. */
  2056. int drm_mode_getplane(struct drm_device *dev, void *data,
  2057. struct drm_file *file_priv)
  2058. {
  2059. struct drm_mode_get_plane *plane_resp = data;
  2060. struct drm_plane *plane;
  2061. uint32_t __user *format_ptr;
  2062. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2063. return -EINVAL;
  2064. plane = drm_plane_find(dev, plane_resp->plane_id);
  2065. if (!plane)
  2066. return -ENOENT;
  2067. drm_modeset_lock(&plane->mutex, NULL);
  2068. if (plane->crtc)
  2069. plane_resp->crtc_id = plane->crtc->base.id;
  2070. else
  2071. plane_resp->crtc_id = 0;
  2072. if (plane->fb)
  2073. plane_resp->fb_id = plane->fb->base.id;
  2074. else
  2075. plane_resp->fb_id = 0;
  2076. drm_modeset_unlock(&plane->mutex);
  2077. plane_resp->plane_id = plane->base.id;
  2078. plane_resp->possible_crtcs = plane->possible_crtcs;
  2079. plane_resp->gamma_size = 0;
  2080. /*
  2081. * This ioctl is called twice, once to determine how much space is
  2082. * needed, and the 2nd time to fill it.
  2083. */
  2084. if (plane->format_count &&
  2085. (plane_resp->count_format_types >= plane->format_count)) {
  2086. format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  2087. if (copy_to_user(format_ptr,
  2088. plane->format_types,
  2089. sizeof(uint32_t) * plane->format_count)) {
  2090. return -EFAULT;
  2091. }
  2092. }
  2093. plane_resp->count_format_types = plane->format_count;
  2094. return 0;
  2095. }
  2096. /**
  2097. * drm_plane_check_pixel_format - Check if the plane supports the pixel format
  2098. * @plane: plane to check for format support
  2099. * @format: the pixel format
  2100. *
  2101. * Returns:
  2102. * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
  2103. * otherwise.
  2104. */
  2105. int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
  2106. {
  2107. unsigned int i;
  2108. for (i = 0; i < plane->format_count; i++) {
  2109. if (format == plane->format_types[i])
  2110. return 0;
  2111. }
  2112. return -EINVAL;
  2113. }
  2114. static int check_src_coords(uint32_t src_x, uint32_t src_y,
  2115. uint32_t src_w, uint32_t src_h,
  2116. const struct drm_framebuffer *fb)
  2117. {
  2118. unsigned int fb_width, fb_height;
  2119. fb_width = fb->width << 16;
  2120. fb_height = fb->height << 16;
  2121. /* Make sure source coordinates are inside the fb. */
  2122. if (src_w > fb_width ||
  2123. src_x > fb_width - src_w ||
  2124. src_h > fb_height ||
  2125. src_y > fb_height - src_h) {
  2126. DRM_DEBUG_KMS("Invalid source coordinates "
  2127. "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2128. src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2129. src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2130. src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2131. src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2132. return -ENOSPC;
  2133. }
  2134. return 0;
  2135. }
  2136. /*
  2137. * setplane_internal - setplane handler for internal callers
  2138. *
  2139. * Note that we assume an extra reference has already been taken on fb. If the
  2140. * update fails, this reference will be dropped before return; if it succeeds,
  2141. * the previous framebuffer (if any) will be unreferenced instead.
  2142. *
  2143. * src_{x,y,w,h} are provided in 16.16 fixed point format
  2144. */
  2145. static int __setplane_internal(struct drm_plane *plane,
  2146. struct drm_crtc *crtc,
  2147. struct drm_framebuffer *fb,
  2148. int32_t crtc_x, int32_t crtc_y,
  2149. uint32_t crtc_w, uint32_t crtc_h,
  2150. /* src_{x,y,w,h} values are 16.16 fixed point */
  2151. uint32_t src_x, uint32_t src_y,
  2152. uint32_t src_w, uint32_t src_h)
  2153. {
  2154. int ret = 0;
  2155. /* No fb means shut it down */
  2156. if (!fb) {
  2157. plane->old_fb = plane->fb;
  2158. ret = plane->funcs->disable_plane(plane);
  2159. if (!ret) {
  2160. plane->crtc = NULL;
  2161. plane->fb = NULL;
  2162. } else {
  2163. plane->old_fb = NULL;
  2164. }
  2165. goto out;
  2166. }
  2167. /* Check whether this plane is usable on this CRTC */
  2168. if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  2169. DRM_DEBUG_KMS("Invalid crtc for plane\n");
  2170. ret = -EINVAL;
  2171. goto out;
  2172. }
  2173. /* Check whether this plane supports the fb pixel format. */
  2174. ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
  2175. if (ret) {
  2176. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2177. drm_get_format_name(fb->pixel_format));
  2178. goto out;
  2179. }
  2180. /* Give drivers some help against integer overflows */
  2181. if (crtc_w > INT_MAX ||
  2182. crtc_x > INT_MAX - (int32_t) crtc_w ||
  2183. crtc_h > INT_MAX ||
  2184. crtc_y > INT_MAX - (int32_t) crtc_h) {
  2185. DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2186. crtc_w, crtc_h, crtc_x, crtc_y);
  2187. ret = -ERANGE;
  2188. goto out;
  2189. }
  2190. ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
  2191. if (ret)
  2192. goto out;
  2193. plane->old_fb = plane->fb;
  2194. ret = plane->funcs->update_plane(plane, crtc, fb,
  2195. crtc_x, crtc_y, crtc_w, crtc_h,
  2196. src_x, src_y, src_w, src_h);
  2197. if (!ret) {
  2198. plane->crtc = crtc;
  2199. plane->fb = fb;
  2200. fb = NULL;
  2201. } else {
  2202. plane->old_fb = NULL;
  2203. }
  2204. out:
  2205. if (fb)
  2206. drm_framebuffer_unreference(fb);
  2207. if (plane->old_fb)
  2208. drm_framebuffer_unreference(plane->old_fb);
  2209. plane->old_fb = NULL;
  2210. return ret;
  2211. }
  2212. static int setplane_internal(struct drm_plane *plane,
  2213. struct drm_crtc *crtc,
  2214. struct drm_framebuffer *fb,
  2215. int32_t crtc_x, int32_t crtc_y,
  2216. uint32_t crtc_w, uint32_t crtc_h,
  2217. /* src_{x,y,w,h} values are 16.16 fixed point */
  2218. uint32_t src_x, uint32_t src_y,
  2219. uint32_t src_w, uint32_t src_h)
  2220. {
  2221. int ret;
  2222. drm_modeset_lock_all(plane->dev);
  2223. ret = __setplane_internal(plane, crtc, fb,
  2224. crtc_x, crtc_y, crtc_w, crtc_h,
  2225. src_x, src_y, src_w, src_h);
  2226. drm_modeset_unlock_all(plane->dev);
  2227. return ret;
  2228. }
  2229. /**
  2230. * drm_mode_setplane - configure a plane's configuration
  2231. * @dev: DRM device
  2232. * @data: ioctl data*
  2233. * @file_priv: DRM file info
  2234. *
  2235. * Set plane configuration, including placement, fb, scaling, and other factors.
  2236. * Or pass a NULL fb to disable (planes may be disabled without providing a
  2237. * valid crtc).
  2238. *
  2239. * Returns:
  2240. * Zero on success, negative errno on failure.
  2241. */
  2242. int drm_mode_setplane(struct drm_device *dev, void *data,
  2243. struct drm_file *file_priv)
  2244. {
  2245. struct drm_mode_set_plane *plane_req = data;
  2246. struct drm_plane *plane;
  2247. struct drm_crtc *crtc = NULL;
  2248. struct drm_framebuffer *fb = NULL;
  2249. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2250. return -EINVAL;
  2251. /*
  2252. * First, find the plane, crtc, and fb objects. If not available,
  2253. * we don't bother to call the driver.
  2254. */
  2255. plane = drm_plane_find(dev, plane_req->plane_id);
  2256. if (!plane) {
  2257. DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2258. plane_req->plane_id);
  2259. return -ENOENT;
  2260. }
  2261. if (plane_req->fb_id) {
  2262. fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2263. if (!fb) {
  2264. DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2265. plane_req->fb_id);
  2266. return -ENOENT;
  2267. }
  2268. crtc = drm_crtc_find(dev, plane_req->crtc_id);
  2269. if (!crtc) {
  2270. DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2271. plane_req->crtc_id);
  2272. return -ENOENT;
  2273. }
  2274. }
  2275. /*
  2276. * setplane_internal will take care of deref'ing either the old or new
  2277. * framebuffer depending on success.
  2278. */
  2279. return setplane_internal(plane, crtc, fb,
  2280. plane_req->crtc_x, plane_req->crtc_y,
  2281. plane_req->crtc_w, plane_req->crtc_h,
  2282. plane_req->src_x, plane_req->src_y,
  2283. plane_req->src_w, plane_req->src_h);
  2284. }
  2285. /**
  2286. * drm_mode_set_config_internal - helper to call ->set_config
  2287. * @set: modeset config to set
  2288. *
  2289. * This is a little helper to wrap internal calls to the ->set_config driver
  2290. * interface. The only thing it adds is correct refcounting dance.
  2291. *
  2292. * Returns:
  2293. * Zero on success, negative errno on failure.
  2294. */
  2295. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2296. {
  2297. struct drm_crtc *crtc = set->crtc;
  2298. struct drm_framebuffer *fb;
  2299. struct drm_crtc *tmp;
  2300. int ret;
  2301. /*
  2302. * NOTE: ->set_config can also disable other crtcs (if we steal all
  2303. * connectors from it), hence we need to refcount the fbs across all
  2304. * crtcs. Atomic modeset will have saner semantics ...
  2305. */
  2306. drm_for_each_crtc(tmp, crtc->dev)
  2307. tmp->primary->old_fb = tmp->primary->fb;
  2308. fb = set->fb;
  2309. ret = crtc->funcs->set_config(set);
  2310. if (ret == 0) {
  2311. crtc->primary->crtc = crtc;
  2312. crtc->primary->fb = fb;
  2313. }
  2314. drm_for_each_crtc(tmp, crtc->dev) {
  2315. if (tmp->primary->fb)
  2316. drm_framebuffer_reference(tmp->primary->fb);
  2317. if (tmp->primary->old_fb)
  2318. drm_framebuffer_unreference(tmp->primary->old_fb);
  2319. tmp->primary->old_fb = NULL;
  2320. }
  2321. return ret;
  2322. }
  2323. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2324. /**
  2325. * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
  2326. * @mode: mode to query
  2327. * @hdisplay: hdisplay value to fill in
  2328. * @vdisplay: vdisplay value to fill in
  2329. *
  2330. * The vdisplay value will be doubled if the specified mode is a stereo mode of
  2331. * the appropriate layout.
  2332. */
  2333. void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
  2334. int *hdisplay, int *vdisplay)
  2335. {
  2336. struct drm_display_mode adjusted;
  2337. drm_mode_copy(&adjusted, mode);
  2338. drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
  2339. *hdisplay = adjusted.crtc_hdisplay;
  2340. *vdisplay = adjusted.crtc_vdisplay;
  2341. }
  2342. EXPORT_SYMBOL(drm_crtc_get_hv_timing);
  2343. /**
  2344. * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2345. * CRTC viewport
  2346. * @crtc: CRTC that framebuffer will be displayed on
  2347. * @x: x panning
  2348. * @y: y panning
  2349. * @mode: mode that framebuffer will be displayed under
  2350. * @fb: framebuffer to check size of
  2351. */
  2352. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2353. int x, int y,
  2354. const struct drm_display_mode *mode,
  2355. const struct drm_framebuffer *fb)
  2356. {
  2357. int hdisplay, vdisplay;
  2358. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  2359. if (crtc->state &&
  2360. crtc->primary->state->rotation & (BIT(DRM_ROTATE_90) |
  2361. BIT(DRM_ROTATE_270)))
  2362. swap(hdisplay, vdisplay);
  2363. return check_src_coords(x << 16, y << 16,
  2364. hdisplay << 16, vdisplay << 16, fb);
  2365. }
  2366. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2367. /**
  2368. * drm_mode_setcrtc - set CRTC configuration
  2369. * @dev: drm device for the ioctl
  2370. * @data: data pointer for the ioctl
  2371. * @file_priv: drm file for the ioctl call
  2372. *
  2373. * Build a new CRTC configuration based on user request.
  2374. *
  2375. * Called by the user via ioctl.
  2376. *
  2377. * Returns:
  2378. * Zero on success, negative errno on failure.
  2379. */
  2380. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2381. struct drm_file *file_priv)
  2382. {
  2383. struct drm_mode_config *config = &dev->mode_config;
  2384. struct drm_mode_crtc *crtc_req = data;
  2385. struct drm_crtc *crtc;
  2386. struct drm_connector **connector_set = NULL, *connector;
  2387. struct drm_framebuffer *fb = NULL;
  2388. struct drm_display_mode *mode = NULL;
  2389. struct drm_mode_set set;
  2390. uint32_t __user *set_connectors_ptr;
  2391. int ret;
  2392. int i;
  2393. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2394. return -EINVAL;
  2395. /*
  2396. * Universal plane src offsets are only 16.16, prevent havoc for
  2397. * drivers using universal plane code internally.
  2398. */
  2399. if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
  2400. return -ERANGE;
  2401. drm_modeset_lock_all(dev);
  2402. crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2403. if (!crtc) {
  2404. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2405. ret = -ENOENT;
  2406. goto out;
  2407. }
  2408. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  2409. if (crtc_req->mode_valid) {
  2410. /* If we have a mode we need a framebuffer. */
  2411. /* If we pass -1, set the mode with the currently bound fb */
  2412. if (crtc_req->fb_id == -1) {
  2413. if (!crtc->primary->fb) {
  2414. DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2415. ret = -EINVAL;
  2416. goto out;
  2417. }
  2418. fb = crtc->primary->fb;
  2419. /* Make refcounting symmetric with the lookup path. */
  2420. drm_framebuffer_reference(fb);
  2421. } else {
  2422. fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2423. if (!fb) {
  2424. DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2425. crtc_req->fb_id);
  2426. ret = -ENOENT;
  2427. goto out;
  2428. }
  2429. }
  2430. mode = drm_mode_create(dev);
  2431. if (!mode) {
  2432. ret = -ENOMEM;
  2433. goto out;
  2434. }
  2435. ret = drm_mode_convert_umode(mode, &crtc_req->mode);
  2436. if (ret) {
  2437. DRM_DEBUG_KMS("Invalid mode\n");
  2438. goto out;
  2439. }
  2440. drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  2441. /*
  2442. * Check whether the primary plane supports the fb pixel format.
  2443. * Drivers not implementing the universal planes API use a
  2444. * default formats list provided by the DRM core which doesn't
  2445. * match real hardware capabilities. Skip the check in that
  2446. * case.
  2447. */
  2448. if (!crtc->primary->format_default) {
  2449. ret = drm_plane_check_pixel_format(crtc->primary,
  2450. fb->pixel_format);
  2451. if (ret) {
  2452. DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2453. drm_get_format_name(fb->pixel_format));
  2454. goto out;
  2455. }
  2456. }
  2457. ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2458. mode, fb);
  2459. if (ret)
  2460. goto out;
  2461. }
  2462. if (crtc_req->count_connectors == 0 && mode) {
  2463. DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2464. ret = -EINVAL;
  2465. goto out;
  2466. }
  2467. if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2468. DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2469. crtc_req->count_connectors);
  2470. ret = -EINVAL;
  2471. goto out;
  2472. }
  2473. if (crtc_req->count_connectors > 0) {
  2474. u32 out_id;
  2475. /* Avoid unbounded kernel memory allocation */
  2476. if (crtc_req->count_connectors > config->num_connector) {
  2477. ret = -EINVAL;
  2478. goto out;
  2479. }
  2480. connector_set = kmalloc_array(crtc_req->count_connectors,
  2481. sizeof(struct drm_connector *),
  2482. GFP_KERNEL);
  2483. if (!connector_set) {
  2484. ret = -ENOMEM;
  2485. goto out;
  2486. }
  2487. for (i = 0; i < crtc_req->count_connectors; i++) {
  2488. connector_set[i] = NULL;
  2489. set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2490. if (get_user(out_id, &set_connectors_ptr[i])) {
  2491. ret = -EFAULT;
  2492. goto out;
  2493. }
  2494. connector = drm_connector_lookup(dev, out_id);
  2495. if (!connector) {
  2496. DRM_DEBUG_KMS("Connector id %d unknown\n",
  2497. out_id);
  2498. ret = -ENOENT;
  2499. goto out;
  2500. }
  2501. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2502. connector->base.id,
  2503. connector->name);
  2504. connector_set[i] = connector;
  2505. }
  2506. }
  2507. set.crtc = crtc;
  2508. set.x = crtc_req->x;
  2509. set.y = crtc_req->y;
  2510. set.mode = mode;
  2511. set.connectors = connector_set;
  2512. set.num_connectors = crtc_req->count_connectors;
  2513. set.fb = fb;
  2514. ret = drm_mode_set_config_internal(&set);
  2515. out:
  2516. if (fb)
  2517. drm_framebuffer_unreference(fb);
  2518. if (connector_set) {
  2519. for (i = 0; i < crtc_req->count_connectors; i++) {
  2520. if (connector_set[i])
  2521. drm_connector_unreference(connector_set[i]);
  2522. }
  2523. }
  2524. kfree(connector_set);
  2525. drm_mode_destroy(dev, mode);
  2526. drm_modeset_unlock_all(dev);
  2527. return ret;
  2528. }
  2529. /**
  2530. * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
  2531. * universal plane handler call
  2532. * @crtc: crtc to update cursor for
  2533. * @req: data pointer for the ioctl
  2534. * @file_priv: drm file for the ioctl call
  2535. *
  2536. * Legacy cursor ioctl's work directly with driver buffer handles. To
  2537. * translate legacy ioctl calls into universal plane handler calls, we need to
  2538. * wrap the native buffer handle in a drm_framebuffer.
  2539. *
  2540. * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
  2541. * buffer with a pitch of 4*width; the universal plane interface should be used
  2542. * directly in cases where the hardware can support other buffer settings and
  2543. * userspace wants to make use of these capabilities.
  2544. *
  2545. * Returns:
  2546. * Zero on success, negative errno on failure.
  2547. */
  2548. static int drm_mode_cursor_universal(struct drm_crtc *crtc,
  2549. struct drm_mode_cursor2 *req,
  2550. struct drm_file *file_priv)
  2551. {
  2552. struct drm_device *dev = crtc->dev;
  2553. struct drm_framebuffer *fb = NULL;
  2554. struct drm_mode_fb_cmd2 fbreq = {
  2555. .width = req->width,
  2556. .height = req->height,
  2557. .pixel_format = DRM_FORMAT_ARGB8888,
  2558. .pitches = { req->width * 4 },
  2559. .handles = { req->handle },
  2560. };
  2561. int32_t crtc_x, crtc_y;
  2562. uint32_t crtc_w = 0, crtc_h = 0;
  2563. uint32_t src_w = 0, src_h = 0;
  2564. int ret = 0;
  2565. BUG_ON(!crtc->cursor);
  2566. WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
  2567. /*
  2568. * Obtain fb we'll be using (either new or existing) and take an extra
  2569. * reference to it if fb != null. setplane will take care of dropping
  2570. * the reference if the plane update fails.
  2571. */
  2572. if (req->flags & DRM_MODE_CURSOR_BO) {
  2573. if (req->handle) {
  2574. fb = internal_framebuffer_create(dev, &fbreq, file_priv);
  2575. if (IS_ERR(fb)) {
  2576. DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
  2577. return PTR_ERR(fb);
  2578. }
  2579. } else {
  2580. fb = NULL;
  2581. }
  2582. } else {
  2583. fb = crtc->cursor->fb;
  2584. if (fb)
  2585. drm_framebuffer_reference(fb);
  2586. }
  2587. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2588. crtc_x = req->x;
  2589. crtc_y = req->y;
  2590. } else {
  2591. crtc_x = crtc->cursor_x;
  2592. crtc_y = crtc->cursor_y;
  2593. }
  2594. if (fb) {
  2595. crtc_w = fb->width;
  2596. crtc_h = fb->height;
  2597. src_w = fb->width << 16;
  2598. src_h = fb->height << 16;
  2599. }
  2600. /*
  2601. * setplane_internal will take care of deref'ing either the old or new
  2602. * framebuffer depending on success.
  2603. */
  2604. ret = __setplane_internal(crtc->cursor, crtc, fb,
  2605. crtc_x, crtc_y, crtc_w, crtc_h,
  2606. 0, 0, src_w, src_h);
  2607. /* Update successful; save new cursor position, if necessary */
  2608. if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
  2609. crtc->cursor_x = req->x;
  2610. crtc->cursor_y = req->y;
  2611. }
  2612. return ret;
  2613. }
  2614. static int drm_mode_cursor_common(struct drm_device *dev,
  2615. struct drm_mode_cursor2 *req,
  2616. struct drm_file *file_priv)
  2617. {
  2618. struct drm_crtc *crtc;
  2619. int ret = 0;
  2620. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2621. return -EINVAL;
  2622. if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2623. return -EINVAL;
  2624. crtc = drm_crtc_find(dev, req->crtc_id);
  2625. if (!crtc) {
  2626. DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2627. return -ENOENT;
  2628. }
  2629. /*
  2630. * If this crtc has a universal cursor plane, call that plane's update
  2631. * handler rather than using legacy cursor handlers.
  2632. */
  2633. drm_modeset_lock_crtc(crtc, crtc->cursor);
  2634. if (crtc->cursor) {
  2635. ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2636. goto out;
  2637. }
  2638. if (req->flags & DRM_MODE_CURSOR_BO) {
  2639. if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2640. ret = -ENXIO;
  2641. goto out;
  2642. }
  2643. /* Turns off the cursor if handle is 0 */
  2644. if (crtc->funcs->cursor_set2)
  2645. ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2646. req->width, req->height, req->hot_x, req->hot_y);
  2647. else
  2648. ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2649. req->width, req->height);
  2650. }
  2651. if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2652. if (crtc->funcs->cursor_move) {
  2653. ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2654. } else {
  2655. ret = -EFAULT;
  2656. goto out;
  2657. }
  2658. }
  2659. out:
  2660. drm_modeset_unlock_crtc(crtc);
  2661. return ret;
  2662. }
  2663. /**
  2664. * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2665. * @dev: drm device for the ioctl
  2666. * @data: data pointer for the ioctl
  2667. * @file_priv: drm file for the ioctl call
  2668. *
  2669. * Set the cursor configuration based on user request.
  2670. *
  2671. * Called by the user via ioctl.
  2672. *
  2673. * Returns:
  2674. * Zero on success, negative errno on failure.
  2675. */
  2676. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2677. void *data, struct drm_file *file_priv)
  2678. {
  2679. struct drm_mode_cursor *req = data;
  2680. struct drm_mode_cursor2 new_req;
  2681. memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2682. new_req.hot_x = new_req.hot_y = 0;
  2683. return drm_mode_cursor_common(dev, &new_req, file_priv);
  2684. }
  2685. /**
  2686. * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2687. * @dev: drm device for the ioctl
  2688. * @data: data pointer for the ioctl
  2689. * @file_priv: drm file for the ioctl call
  2690. *
  2691. * Set the cursor configuration based on user request. This implements the 2nd
  2692. * version of the cursor ioctl, which allows userspace to additionally specify
  2693. * the hotspot of the pointer.
  2694. *
  2695. * Called by the user via ioctl.
  2696. *
  2697. * Returns:
  2698. * Zero on success, negative errno on failure.
  2699. */
  2700. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2701. void *data, struct drm_file *file_priv)
  2702. {
  2703. struct drm_mode_cursor2 *req = data;
  2704. return drm_mode_cursor_common(dev, req, file_priv);
  2705. }
  2706. /**
  2707. * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2708. * @bpp: bits per pixels
  2709. * @depth: bit depth per pixel
  2710. *
  2711. * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2712. * Useful in fbdev emulation code, since that deals in those values.
  2713. */
  2714. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2715. {
  2716. uint32_t fmt;
  2717. switch (bpp) {
  2718. case 8:
  2719. fmt = DRM_FORMAT_C8;
  2720. break;
  2721. case 16:
  2722. if (depth == 15)
  2723. fmt = DRM_FORMAT_XRGB1555;
  2724. else
  2725. fmt = DRM_FORMAT_RGB565;
  2726. break;
  2727. case 24:
  2728. fmt = DRM_FORMAT_RGB888;
  2729. break;
  2730. case 32:
  2731. if (depth == 24)
  2732. fmt = DRM_FORMAT_XRGB8888;
  2733. else if (depth == 30)
  2734. fmt = DRM_FORMAT_XRGB2101010;
  2735. else
  2736. fmt = DRM_FORMAT_ARGB8888;
  2737. break;
  2738. default:
  2739. DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2740. fmt = DRM_FORMAT_XRGB8888;
  2741. break;
  2742. }
  2743. return fmt;
  2744. }
  2745. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2746. /**
  2747. * drm_mode_addfb - add an FB to the graphics configuration
  2748. * @dev: drm device for the ioctl
  2749. * @data: data pointer for the ioctl
  2750. * @file_priv: drm file for the ioctl call
  2751. *
  2752. * Add a new FB to the specified CRTC, given a user request. This is the
  2753. * original addfb ioctl which only supported RGB formats.
  2754. *
  2755. * Called by the user via ioctl.
  2756. *
  2757. * Returns:
  2758. * Zero on success, negative errno on failure.
  2759. */
  2760. int drm_mode_addfb(struct drm_device *dev,
  2761. void *data, struct drm_file *file_priv)
  2762. {
  2763. struct drm_mode_fb_cmd *or = data;
  2764. struct drm_mode_fb_cmd2 r = {};
  2765. int ret;
  2766. /* convert to new format and call new ioctl */
  2767. r.fb_id = or->fb_id;
  2768. r.width = or->width;
  2769. r.height = or->height;
  2770. r.pitches[0] = or->pitch;
  2771. r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2772. r.handles[0] = or->handle;
  2773. ret = drm_mode_addfb2(dev, &r, file_priv);
  2774. if (ret)
  2775. return ret;
  2776. or->fb_id = r.fb_id;
  2777. return 0;
  2778. }
  2779. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2780. {
  2781. uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2782. switch (format) {
  2783. case DRM_FORMAT_C8:
  2784. case DRM_FORMAT_RGB332:
  2785. case DRM_FORMAT_BGR233:
  2786. case DRM_FORMAT_XRGB4444:
  2787. case DRM_FORMAT_XBGR4444:
  2788. case DRM_FORMAT_RGBX4444:
  2789. case DRM_FORMAT_BGRX4444:
  2790. case DRM_FORMAT_ARGB4444:
  2791. case DRM_FORMAT_ABGR4444:
  2792. case DRM_FORMAT_RGBA4444:
  2793. case DRM_FORMAT_BGRA4444:
  2794. case DRM_FORMAT_XRGB1555:
  2795. case DRM_FORMAT_XBGR1555:
  2796. case DRM_FORMAT_RGBX5551:
  2797. case DRM_FORMAT_BGRX5551:
  2798. case DRM_FORMAT_ARGB1555:
  2799. case DRM_FORMAT_ABGR1555:
  2800. case DRM_FORMAT_RGBA5551:
  2801. case DRM_FORMAT_BGRA5551:
  2802. case DRM_FORMAT_RGB565:
  2803. case DRM_FORMAT_BGR565:
  2804. case DRM_FORMAT_RGB888:
  2805. case DRM_FORMAT_BGR888:
  2806. case DRM_FORMAT_XRGB8888:
  2807. case DRM_FORMAT_XBGR8888:
  2808. case DRM_FORMAT_RGBX8888:
  2809. case DRM_FORMAT_BGRX8888:
  2810. case DRM_FORMAT_ARGB8888:
  2811. case DRM_FORMAT_ABGR8888:
  2812. case DRM_FORMAT_RGBA8888:
  2813. case DRM_FORMAT_BGRA8888:
  2814. case DRM_FORMAT_XRGB2101010:
  2815. case DRM_FORMAT_XBGR2101010:
  2816. case DRM_FORMAT_RGBX1010102:
  2817. case DRM_FORMAT_BGRX1010102:
  2818. case DRM_FORMAT_ARGB2101010:
  2819. case DRM_FORMAT_ABGR2101010:
  2820. case DRM_FORMAT_RGBA1010102:
  2821. case DRM_FORMAT_BGRA1010102:
  2822. case DRM_FORMAT_YUYV:
  2823. case DRM_FORMAT_YVYU:
  2824. case DRM_FORMAT_UYVY:
  2825. case DRM_FORMAT_VYUY:
  2826. case DRM_FORMAT_AYUV:
  2827. case DRM_FORMAT_NV12:
  2828. case DRM_FORMAT_NV21:
  2829. case DRM_FORMAT_NV16:
  2830. case DRM_FORMAT_NV61:
  2831. case DRM_FORMAT_NV24:
  2832. case DRM_FORMAT_NV42:
  2833. case DRM_FORMAT_YUV410:
  2834. case DRM_FORMAT_YVU410:
  2835. case DRM_FORMAT_YUV411:
  2836. case DRM_FORMAT_YVU411:
  2837. case DRM_FORMAT_YUV420:
  2838. case DRM_FORMAT_YVU420:
  2839. case DRM_FORMAT_YUV422:
  2840. case DRM_FORMAT_YVU422:
  2841. case DRM_FORMAT_YUV444:
  2842. case DRM_FORMAT_YVU444:
  2843. return 0;
  2844. default:
  2845. DRM_DEBUG_KMS("invalid pixel format %s\n",
  2846. drm_get_format_name(r->pixel_format));
  2847. return -EINVAL;
  2848. }
  2849. }
  2850. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2851. {
  2852. int ret, hsub, vsub, num_planes, i;
  2853. ret = format_check(r);
  2854. if (ret) {
  2855. DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2856. drm_get_format_name(r->pixel_format));
  2857. return ret;
  2858. }
  2859. hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2860. vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2861. num_planes = drm_format_num_planes(r->pixel_format);
  2862. if (r->width == 0 || r->width % hsub) {
  2863. DRM_DEBUG_KMS("bad framebuffer width %u\n", r->width);
  2864. return -EINVAL;
  2865. }
  2866. if (r->height == 0 || r->height % vsub) {
  2867. DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2868. return -EINVAL;
  2869. }
  2870. for (i = 0; i < num_planes; i++) {
  2871. unsigned int width = r->width / (i != 0 ? hsub : 1);
  2872. unsigned int height = r->height / (i != 0 ? vsub : 1);
  2873. unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2874. if (!r->handles[i]) {
  2875. DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2876. return -EINVAL;
  2877. }
  2878. if ((uint64_t) width * cpp > UINT_MAX)
  2879. return -ERANGE;
  2880. if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2881. return -ERANGE;
  2882. if (r->pitches[i] < width * cpp) {
  2883. DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2884. return -EINVAL;
  2885. }
  2886. if (r->modifier[i] && !(r->flags & DRM_MODE_FB_MODIFIERS)) {
  2887. DRM_DEBUG_KMS("bad fb modifier %llu for plane %d\n",
  2888. r->modifier[i], i);
  2889. return -EINVAL;
  2890. }
  2891. /* modifier specific checks: */
  2892. switch (r->modifier[i]) {
  2893. case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  2894. /* NOTE: the pitch restriction may be lifted later if it turns
  2895. * out that no hw has this restriction:
  2896. */
  2897. if (r->pixel_format != DRM_FORMAT_NV12 ||
  2898. width % 128 || height % 32 ||
  2899. r->pitches[i] % 128) {
  2900. DRM_DEBUG_KMS("bad modifier data for plane %d\n", i);
  2901. return -EINVAL;
  2902. }
  2903. break;
  2904. default:
  2905. break;
  2906. }
  2907. }
  2908. for (i = num_planes; i < 4; i++) {
  2909. if (r->modifier[i]) {
  2910. DRM_DEBUG_KMS("non-zero modifier for unused plane %d\n", i);
  2911. return -EINVAL;
  2912. }
  2913. /* Pre-FB_MODIFIERS userspace didn't clear the structs properly. */
  2914. if (!(r->flags & DRM_MODE_FB_MODIFIERS))
  2915. continue;
  2916. if (r->handles[i]) {
  2917. DRM_DEBUG_KMS("buffer object handle for unused plane %d\n", i);
  2918. return -EINVAL;
  2919. }
  2920. if (r->pitches[i]) {
  2921. DRM_DEBUG_KMS("non-zero pitch for unused plane %d\n", i);
  2922. return -EINVAL;
  2923. }
  2924. if (r->offsets[i]) {
  2925. DRM_DEBUG_KMS("non-zero offset for unused plane %d\n", i);
  2926. return -EINVAL;
  2927. }
  2928. }
  2929. return 0;
  2930. }
  2931. static struct drm_framebuffer *
  2932. internal_framebuffer_create(struct drm_device *dev,
  2933. const struct drm_mode_fb_cmd2 *r,
  2934. struct drm_file *file_priv)
  2935. {
  2936. struct drm_mode_config *config = &dev->mode_config;
  2937. struct drm_framebuffer *fb;
  2938. int ret;
  2939. if (r->flags & ~(DRM_MODE_FB_INTERLACED | DRM_MODE_FB_MODIFIERS)) {
  2940. DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2941. return ERR_PTR(-EINVAL);
  2942. }
  2943. if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2944. DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2945. r->width, config->min_width, config->max_width);
  2946. return ERR_PTR(-EINVAL);
  2947. }
  2948. if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2949. DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  2950. r->height, config->min_height, config->max_height);
  2951. return ERR_PTR(-EINVAL);
  2952. }
  2953. if (r->flags & DRM_MODE_FB_MODIFIERS &&
  2954. !dev->mode_config.allow_fb_modifiers) {
  2955. DRM_DEBUG_KMS("driver does not support fb modifiers\n");
  2956. return ERR_PTR(-EINVAL);
  2957. }
  2958. ret = framebuffer_check(r);
  2959. if (ret)
  2960. return ERR_PTR(ret);
  2961. fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  2962. if (IS_ERR(fb)) {
  2963. DRM_DEBUG_KMS("could not create framebuffer\n");
  2964. return fb;
  2965. }
  2966. return fb;
  2967. }
  2968. /**
  2969. * drm_mode_addfb2 - add an FB to the graphics configuration
  2970. * @dev: drm device for the ioctl
  2971. * @data: data pointer for the ioctl
  2972. * @file_priv: drm file for the ioctl call
  2973. *
  2974. * Add a new FB to the specified CRTC, given a user request with format. This is
  2975. * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  2976. * and uses fourcc codes as pixel format specifiers.
  2977. *
  2978. * Called by the user via ioctl.
  2979. *
  2980. * Returns:
  2981. * Zero on success, negative errno on failure.
  2982. */
  2983. int drm_mode_addfb2(struct drm_device *dev,
  2984. void *data, struct drm_file *file_priv)
  2985. {
  2986. struct drm_mode_fb_cmd2 *r = data;
  2987. struct drm_framebuffer *fb;
  2988. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2989. return -EINVAL;
  2990. fb = internal_framebuffer_create(dev, r, file_priv);
  2991. if (IS_ERR(fb))
  2992. return PTR_ERR(fb);
  2993. DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2994. r->fb_id = fb->base.id;
  2995. /* Transfer ownership to the filp for reaping on close */
  2996. mutex_lock(&file_priv->fbs_lock);
  2997. list_add(&fb->filp_head, &file_priv->fbs);
  2998. mutex_unlock(&file_priv->fbs_lock);
  2999. return 0;
  3000. }
  3001. struct drm_mode_rmfb_work {
  3002. struct work_struct work;
  3003. struct list_head fbs;
  3004. };
  3005. static void drm_mode_rmfb_work_fn(struct work_struct *w)
  3006. {
  3007. struct drm_mode_rmfb_work *arg = container_of(w, typeof(*arg), work);
  3008. while (!list_empty(&arg->fbs)) {
  3009. struct drm_framebuffer *fb =
  3010. list_first_entry(&arg->fbs, typeof(*fb), filp_head);
  3011. list_del_init(&fb->filp_head);
  3012. drm_framebuffer_remove(fb);
  3013. }
  3014. }
  3015. /**
  3016. * drm_mode_rmfb - remove an FB from the configuration
  3017. * @dev: drm device for the ioctl
  3018. * @data: data pointer for the ioctl
  3019. * @file_priv: drm file for the ioctl call
  3020. *
  3021. * Remove the FB specified by the user.
  3022. *
  3023. * Called by the user via ioctl.
  3024. *
  3025. * Returns:
  3026. * Zero on success, negative errno on failure.
  3027. */
  3028. int drm_mode_rmfb(struct drm_device *dev,
  3029. void *data, struct drm_file *file_priv)
  3030. {
  3031. struct drm_framebuffer *fb = NULL;
  3032. struct drm_framebuffer *fbl = NULL;
  3033. uint32_t *id = data;
  3034. int found = 0;
  3035. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3036. return -EINVAL;
  3037. fb = drm_framebuffer_lookup(dev, *id);
  3038. if (!fb)
  3039. return -ENOENT;
  3040. mutex_lock(&file_priv->fbs_lock);
  3041. list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  3042. if (fb == fbl)
  3043. found = 1;
  3044. if (!found) {
  3045. mutex_unlock(&file_priv->fbs_lock);
  3046. goto fail_unref;
  3047. }
  3048. list_del_init(&fb->filp_head);
  3049. mutex_unlock(&file_priv->fbs_lock);
  3050. /* drop the reference we picked up in framebuffer lookup */
  3051. drm_framebuffer_unreference(fb);
  3052. /*
  3053. * we now own the reference that was stored in the fbs list
  3054. *
  3055. * drm_framebuffer_remove may fail with -EINTR on pending signals,
  3056. * so run this in a separate stack as there's no way to correctly
  3057. * handle this after the fb is already removed from the lookup table.
  3058. */
  3059. if (drm_framebuffer_read_refcount(fb) > 1) {
  3060. struct drm_mode_rmfb_work arg;
  3061. INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
  3062. INIT_LIST_HEAD(&arg.fbs);
  3063. list_add_tail(&fb->filp_head, &arg.fbs);
  3064. schedule_work(&arg.work);
  3065. flush_work(&arg.work);
  3066. destroy_work_on_stack(&arg.work);
  3067. } else
  3068. drm_framebuffer_unreference(fb);
  3069. return 0;
  3070. fail_unref:
  3071. drm_framebuffer_unreference(fb);
  3072. return -ENOENT;
  3073. }
  3074. /**
  3075. * drm_mode_getfb - get FB info
  3076. * @dev: drm device for the ioctl
  3077. * @data: data pointer for the ioctl
  3078. * @file_priv: drm file for the ioctl call
  3079. *
  3080. * Lookup the FB given its ID and return info about it.
  3081. *
  3082. * Called by the user via ioctl.
  3083. *
  3084. * Returns:
  3085. * Zero on success, negative errno on failure.
  3086. */
  3087. int drm_mode_getfb(struct drm_device *dev,
  3088. void *data, struct drm_file *file_priv)
  3089. {
  3090. struct drm_mode_fb_cmd *r = data;
  3091. struct drm_framebuffer *fb;
  3092. int ret;
  3093. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3094. return -EINVAL;
  3095. fb = drm_framebuffer_lookup(dev, r->fb_id);
  3096. if (!fb)
  3097. return -ENOENT;
  3098. r->height = fb->height;
  3099. r->width = fb->width;
  3100. r->depth = fb->depth;
  3101. r->bpp = fb->bits_per_pixel;
  3102. r->pitch = fb->pitches[0];
  3103. if (fb->funcs->create_handle) {
  3104. if (file_priv->is_master || capable(CAP_SYS_ADMIN) ||
  3105. drm_is_control_client(file_priv)) {
  3106. ret = fb->funcs->create_handle(fb, file_priv,
  3107. &r->handle);
  3108. } else {
  3109. /* GET_FB() is an unprivileged ioctl so we must not
  3110. * return a buffer-handle to non-master processes! For
  3111. * backwards-compatibility reasons, we cannot make
  3112. * GET_FB() privileged, so just return an invalid handle
  3113. * for non-masters. */
  3114. r->handle = 0;
  3115. ret = 0;
  3116. }
  3117. } else {
  3118. ret = -ENODEV;
  3119. }
  3120. drm_framebuffer_unreference(fb);
  3121. return ret;
  3122. }
  3123. /**
  3124. * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  3125. * @dev: drm device for the ioctl
  3126. * @data: data pointer for the ioctl
  3127. * @file_priv: drm file for the ioctl call
  3128. *
  3129. * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  3130. * rectangle list. Generic userspace which does frontbuffer rendering must call
  3131. * this ioctl to flush out the changes on manual-update display outputs, e.g.
  3132. * usb display-link, mipi manual update panels or edp panel self refresh modes.
  3133. *
  3134. * Modesetting drivers which always update the frontbuffer do not need to
  3135. * implement the corresponding ->dirty framebuffer callback.
  3136. *
  3137. * Called by the user via ioctl.
  3138. *
  3139. * Returns:
  3140. * Zero on success, negative errno on failure.
  3141. */
  3142. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  3143. void *data, struct drm_file *file_priv)
  3144. {
  3145. struct drm_clip_rect __user *clips_ptr;
  3146. struct drm_clip_rect *clips = NULL;
  3147. struct drm_mode_fb_dirty_cmd *r = data;
  3148. struct drm_framebuffer *fb;
  3149. unsigned flags;
  3150. int num_clips;
  3151. int ret;
  3152. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3153. return -EINVAL;
  3154. fb = drm_framebuffer_lookup(dev, r->fb_id);
  3155. if (!fb)
  3156. return -ENOENT;
  3157. num_clips = r->num_clips;
  3158. clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  3159. if (!num_clips != !clips_ptr) {
  3160. ret = -EINVAL;
  3161. goto out_err1;
  3162. }
  3163. flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  3164. /* If userspace annotates copy, clips must come in pairs */
  3165. if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  3166. ret = -EINVAL;
  3167. goto out_err1;
  3168. }
  3169. if (num_clips && clips_ptr) {
  3170. if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  3171. ret = -EINVAL;
  3172. goto out_err1;
  3173. }
  3174. clips = kcalloc(num_clips, sizeof(*clips), GFP_KERNEL);
  3175. if (!clips) {
  3176. ret = -ENOMEM;
  3177. goto out_err1;
  3178. }
  3179. ret = copy_from_user(clips, clips_ptr,
  3180. num_clips * sizeof(*clips));
  3181. if (ret) {
  3182. ret = -EFAULT;
  3183. goto out_err2;
  3184. }
  3185. }
  3186. if (fb->funcs->dirty) {
  3187. ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  3188. clips, num_clips);
  3189. } else {
  3190. ret = -ENOSYS;
  3191. }
  3192. out_err2:
  3193. kfree(clips);
  3194. out_err1:
  3195. drm_framebuffer_unreference(fb);
  3196. return ret;
  3197. }
  3198. /**
  3199. * drm_fb_release - remove and free the FBs on this file
  3200. * @priv: drm file for the ioctl
  3201. *
  3202. * Destroy all the FBs associated with @filp.
  3203. *
  3204. * Called by the user via ioctl.
  3205. *
  3206. * Returns:
  3207. * Zero on success, negative errno on failure.
  3208. */
  3209. void drm_fb_release(struct drm_file *priv)
  3210. {
  3211. struct drm_framebuffer *fb, *tfb;
  3212. struct drm_mode_rmfb_work arg;
  3213. INIT_LIST_HEAD(&arg.fbs);
  3214. /*
  3215. * When the file gets released that means no one else can access the fb
  3216. * list any more, so no need to grab fpriv->fbs_lock. And we need to
  3217. * avoid upsetting lockdep since the universal cursor code adds a
  3218. * framebuffer while holding mutex locks.
  3219. *
  3220. * Note that a real deadlock between fpriv->fbs_lock and the modeset
  3221. * locks is impossible here since no one else but this function can get
  3222. * at it any more.
  3223. */
  3224. list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  3225. if (drm_framebuffer_read_refcount(fb) > 1) {
  3226. list_move_tail(&fb->filp_head, &arg.fbs);
  3227. } else {
  3228. list_del_init(&fb->filp_head);
  3229. /* This drops the fpriv->fbs reference. */
  3230. drm_framebuffer_unreference(fb);
  3231. }
  3232. }
  3233. if (!list_empty(&arg.fbs)) {
  3234. INIT_WORK_ONSTACK(&arg.work, drm_mode_rmfb_work_fn);
  3235. schedule_work(&arg.work);
  3236. flush_work(&arg.work);
  3237. destroy_work_on_stack(&arg.work);
  3238. }
  3239. }
  3240. /**
  3241. * drm_property_create - create a new property type
  3242. * @dev: drm device
  3243. * @flags: flags specifying the property type
  3244. * @name: name of the property
  3245. * @num_values: number of pre-defined values
  3246. *
  3247. * This creates a new generic drm property which can then be attached to a drm
  3248. * object with drm_object_attach_property. The returned property object must be
  3249. * freed with drm_property_destroy.
  3250. *
  3251. * Note that the DRM core keeps a per-device list of properties and that, if
  3252. * drm_mode_config_cleanup() is called, it will destroy all properties created
  3253. * by the driver.
  3254. *
  3255. * Returns:
  3256. * A pointer to the newly created property on success, NULL on failure.
  3257. */
  3258. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  3259. const char *name, int num_values)
  3260. {
  3261. struct drm_property *property = NULL;
  3262. int ret;
  3263. property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  3264. if (!property)
  3265. return NULL;
  3266. property->dev = dev;
  3267. if (num_values) {
  3268. property->values = kcalloc(num_values, sizeof(uint64_t),
  3269. GFP_KERNEL);
  3270. if (!property->values)
  3271. goto fail;
  3272. }
  3273. ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3274. if (ret)
  3275. goto fail;
  3276. property->flags = flags;
  3277. property->num_values = num_values;
  3278. INIT_LIST_HEAD(&property->enum_list);
  3279. if (name) {
  3280. strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3281. property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3282. }
  3283. list_add_tail(&property->head, &dev->mode_config.property_list);
  3284. WARN_ON(!drm_property_type_valid(property));
  3285. return property;
  3286. fail:
  3287. kfree(property->values);
  3288. kfree(property);
  3289. return NULL;
  3290. }
  3291. EXPORT_SYMBOL(drm_property_create);
  3292. /**
  3293. * drm_property_create_enum - create a new enumeration property type
  3294. * @dev: drm device
  3295. * @flags: flags specifying the property type
  3296. * @name: name of the property
  3297. * @props: enumeration lists with property values
  3298. * @num_values: number of pre-defined values
  3299. *
  3300. * This creates a new generic drm property which can then be attached to a drm
  3301. * object with drm_object_attach_property. The returned property object must be
  3302. * freed with drm_property_destroy.
  3303. *
  3304. * Userspace is only allowed to set one of the predefined values for enumeration
  3305. * properties.
  3306. *
  3307. * Returns:
  3308. * A pointer to the newly created property on success, NULL on failure.
  3309. */
  3310. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3311. const char *name,
  3312. const struct drm_prop_enum_list *props,
  3313. int num_values)
  3314. {
  3315. struct drm_property *property;
  3316. int i, ret;
  3317. flags |= DRM_MODE_PROP_ENUM;
  3318. property = drm_property_create(dev, flags, name, num_values);
  3319. if (!property)
  3320. return NULL;
  3321. for (i = 0; i < num_values; i++) {
  3322. ret = drm_property_add_enum(property, i,
  3323. props[i].type,
  3324. props[i].name);
  3325. if (ret) {
  3326. drm_property_destroy(dev, property);
  3327. return NULL;
  3328. }
  3329. }
  3330. return property;
  3331. }
  3332. EXPORT_SYMBOL(drm_property_create_enum);
  3333. /**
  3334. * drm_property_create_bitmask - create a new bitmask property type
  3335. * @dev: drm device
  3336. * @flags: flags specifying the property type
  3337. * @name: name of the property
  3338. * @props: enumeration lists with property bitflags
  3339. * @num_props: size of the @props array
  3340. * @supported_bits: bitmask of all supported enumeration values
  3341. *
  3342. * This creates a new bitmask drm property which can then be attached to a drm
  3343. * object with drm_object_attach_property. The returned property object must be
  3344. * freed with drm_property_destroy.
  3345. *
  3346. * Compared to plain enumeration properties userspace is allowed to set any
  3347. * or'ed together combination of the predefined property bitflag values
  3348. *
  3349. * Returns:
  3350. * A pointer to the newly created property on success, NULL on failure.
  3351. */
  3352. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3353. int flags, const char *name,
  3354. const struct drm_prop_enum_list *props,
  3355. int num_props,
  3356. uint64_t supported_bits)
  3357. {
  3358. struct drm_property *property;
  3359. int i, ret, index = 0;
  3360. int num_values = hweight64(supported_bits);
  3361. flags |= DRM_MODE_PROP_BITMASK;
  3362. property = drm_property_create(dev, flags, name, num_values);
  3363. if (!property)
  3364. return NULL;
  3365. for (i = 0; i < num_props; i++) {
  3366. if (!(supported_bits & (1ULL << props[i].type)))
  3367. continue;
  3368. if (WARN_ON(index >= num_values)) {
  3369. drm_property_destroy(dev, property);
  3370. return NULL;
  3371. }
  3372. ret = drm_property_add_enum(property, index++,
  3373. props[i].type,
  3374. props[i].name);
  3375. if (ret) {
  3376. drm_property_destroy(dev, property);
  3377. return NULL;
  3378. }
  3379. }
  3380. return property;
  3381. }
  3382. EXPORT_SYMBOL(drm_property_create_bitmask);
  3383. static struct drm_property *property_create_range(struct drm_device *dev,
  3384. int flags, const char *name,
  3385. uint64_t min, uint64_t max)
  3386. {
  3387. struct drm_property *property;
  3388. property = drm_property_create(dev, flags, name, 2);
  3389. if (!property)
  3390. return NULL;
  3391. property->values[0] = min;
  3392. property->values[1] = max;
  3393. return property;
  3394. }
  3395. /**
  3396. * drm_property_create_range - create a new unsigned ranged property type
  3397. * @dev: drm device
  3398. * @flags: flags specifying the property type
  3399. * @name: name of the property
  3400. * @min: minimum value of the property
  3401. * @max: maximum value of the property
  3402. *
  3403. * This creates a new generic drm property which can then be attached to a drm
  3404. * object with drm_object_attach_property. The returned property object must be
  3405. * freed with drm_property_destroy.
  3406. *
  3407. * Userspace is allowed to set any unsigned integer value in the (min, max)
  3408. * range inclusive.
  3409. *
  3410. * Returns:
  3411. * A pointer to the newly created property on success, NULL on failure.
  3412. */
  3413. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3414. const char *name,
  3415. uint64_t min, uint64_t max)
  3416. {
  3417. return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3418. name, min, max);
  3419. }
  3420. EXPORT_SYMBOL(drm_property_create_range);
  3421. /**
  3422. * drm_property_create_signed_range - create a new signed ranged property type
  3423. * @dev: drm device
  3424. * @flags: flags specifying the property type
  3425. * @name: name of the property
  3426. * @min: minimum value of the property
  3427. * @max: maximum value of the property
  3428. *
  3429. * This creates a new generic drm property which can then be attached to a drm
  3430. * object with drm_object_attach_property. The returned property object must be
  3431. * freed with drm_property_destroy.
  3432. *
  3433. * Userspace is allowed to set any signed integer value in the (min, max)
  3434. * range inclusive.
  3435. *
  3436. * Returns:
  3437. * A pointer to the newly created property on success, NULL on failure.
  3438. */
  3439. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3440. int flags, const char *name,
  3441. int64_t min, int64_t max)
  3442. {
  3443. return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3444. name, I642U64(min), I642U64(max));
  3445. }
  3446. EXPORT_SYMBOL(drm_property_create_signed_range);
  3447. /**
  3448. * drm_property_create_object - create a new object property type
  3449. * @dev: drm device
  3450. * @flags: flags specifying the property type
  3451. * @name: name of the property
  3452. * @type: object type from DRM_MODE_OBJECT_* defines
  3453. *
  3454. * This creates a new generic drm property which can then be attached to a drm
  3455. * object with drm_object_attach_property. The returned property object must be
  3456. * freed with drm_property_destroy.
  3457. *
  3458. * Userspace is only allowed to set this to any property value of the given
  3459. * @type. Only useful for atomic properties, which is enforced.
  3460. *
  3461. * Returns:
  3462. * A pointer to the newly created property on success, NULL on failure.
  3463. */
  3464. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3465. int flags, const char *name, uint32_t type)
  3466. {
  3467. struct drm_property *property;
  3468. flags |= DRM_MODE_PROP_OBJECT;
  3469. if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
  3470. return NULL;
  3471. property = drm_property_create(dev, flags, name, 1);
  3472. if (!property)
  3473. return NULL;
  3474. property->values[0] = type;
  3475. return property;
  3476. }
  3477. EXPORT_SYMBOL(drm_property_create_object);
  3478. /**
  3479. * drm_property_create_bool - create a new boolean property type
  3480. * @dev: drm device
  3481. * @flags: flags specifying the property type
  3482. * @name: name of the property
  3483. *
  3484. * This creates a new generic drm property which can then be attached to a drm
  3485. * object with drm_object_attach_property. The returned property object must be
  3486. * freed with drm_property_destroy.
  3487. *
  3488. * This is implemented as a ranged property with only {0, 1} as valid values.
  3489. *
  3490. * Returns:
  3491. * A pointer to the newly created property on success, NULL on failure.
  3492. */
  3493. struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
  3494. const char *name)
  3495. {
  3496. return drm_property_create_range(dev, flags, name, 0, 1);
  3497. }
  3498. EXPORT_SYMBOL(drm_property_create_bool);
  3499. /**
  3500. * drm_property_add_enum - add a possible value to an enumeration property
  3501. * @property: enumeration property to change
  3502. * @index: index of the new enumeration
  3503. * @value: value of the new enumeration
  3504. * @name: symbolic name of the new enumeration
  3505. *
  3506. * This functions adds enumerations to a property.
  3507. *
  3508. * It's use is deprecated, drivers should use one of the more specific helpers
  3509. * to directly create the property with all enumerations already attached.
  3510. *
  3511. * Returns:
  3512. * Zero on success, error code on failure.
  3513. */
  3514. int drm_property_add_enum(struct drm_property *property, int index,
  3515. uint64_t value, const char *name)
  3516. {
  3517. struct drm_property_enum *prop_enum;
  3518. if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3519. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3520. return -EINVAL;
  3521. /*
  3522. * Bitmask enum properties have the additional constraint of values
  3523. * from 0 to 63
  3524. */
  3525. if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3526. (value > 63))
  3527. return -EINVAL;
  3528. if (!list_empty(&property->enum_list)) {
  3529. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3530. if (prop_enum->value == value) {
  3531. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3532. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3533. return 0;
  3534. }
  3535. }
  3536. }
  3537. prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3538. if (!prop_enum)
  3539. return -ENOMEM;
  3540. strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3541. prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3542. prop_enum->value = value;
  3543. property->values[index] = value;
  3544. list_add_tail(&prop_enum->head, &property->enum_list);
  3545. return 0;
  3546. }
  3547. EXPORT_SYMBOL(drm_property_add_enum);
  3548. /**
  3549. * drm_property_destroy - destroy a drm property
  3550. * @dev: drm device
  3551. * @property: property to destry
  3552. *
  3553. * This function frees a property including any attached resources like
  3554. * enumeration values.
  3555. */
  3556. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3557. {
  3558. struct drm_property_enum *prop_enum, *pt;
  3559. list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
  3560. list_del(&prop_enum->head);
  3561. kfree(prop_enum);
  3562. }
  3563. if (property->num_values)
  3564. kfree(property->values);
  3565. drm_mode_object_unregister(dev, &property->base);
  3566. list_del(&property->head);
  3567. kfree(property);
  3568. }
  3569. EXPORT_SYMBOL(drm_property_destroy);
  3570. /**
  3571. * drm_object_attach_property - attach a property to a modeset object
  3572. * @obj: drm modeset object
  3573. * @property: property to attach
  3574. * @init_val: initial value of the property
  3575. *
  3576. * This attaches the given property to the modeset object with the given initial
  3577. * value. Currently this function cannot fail since the properties are stored in
  3578. * a statically sized array.
  3579. */
  3580. void drm_object_attach_property(struct drm_mode_object *obj,
  3581. struct drm_property *property,
  3582. uint64_t init_val)
  3583. {
  3584. int count = obj->properties->count;
  3585. if (count == DRM_OBJECT_MAX_PROPERTY) {
  3586. WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3587. "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3588. "you see this message on the same object type.\n",
  3589. obj->type);
  3590. return;
  3591. }
  3592. obj->properties->properties[count] = property;
  3593. obj->properties->values[count] = init_val;
  3594. obj->properties->count++;
  3595. if (property->flags & DRM_MODE_PROP_ATOMIC)
  3596. obj->properties->atomic_count++;
  3597. }
  3598. EXPORT_SYMBOL(drm_object_attach_property);
  3599. /**
  3600. * drm_object_property_set_value - set the value of a property
  3601. * @obj: drm mode object to set property value for
  3602. * @property: property to set
  3603. * @val: value the property should be set to
  3604. *
  3605. * This functions sets a given property on a given object. This function only
  3606. * changes the software state of the property, it does not call into the
  3607. * driver's ->set_property callback.
  3608. *
  3609. * Returns:
  3610. * Zero on success, error code on failure.
  3611. */
  3612. int drm_object_property_set_value(struct drm_mode_object *obj,
  3613. struct drm_property *property, uint64_t val)
  3614. {
  3615. int i;
  3616. for (i = 0; i < obj->properties->count; i++) {
  3617. if (obj->properties->properties[i] == property) {
  3618. obj->properties->values[i] = val;
  3619. return 0;
  3620. }
  3621. }
  3622. return -EINVAL;
  3623. }
  3624. EXPORT_SYMBOL(drm_object_property_set_value);
  3625. /**
  3626. * drm_object_property_get_value - retrieve the value of a property
  3627. * @obj: drm mode object to get property value from
  3628. * @property: property to retrieve
  3629. * @val: storage for the property value
  3630. *
  3631. * This function retrieves the softare state of the given property for the given
  3632. * property. Since there is no driver callback to retrieve the current property
  3633. * value this might be out of sync with the hardware, depending upon the driver
  3634. * and property.
  3635. *
  3636. * Returns:
  3637. * Zero on success, error code on failure.
  3638. */
  3639. int drm_object_property_get_value(struct drm_mode_object *obj,
  3640. struct drm_property *property, uint64_t *val)
  3641. {
  3642. int i;
  3643. /* read-only properties bypass atomic mechanism and still store
  3644. * their value in obj->properties->values[].. mostly to avoid
  3645. * having to deal w/ EDID and similar props in atomic paths:
  3646. */
  3647. if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
  3648. !(property->flags & DRM_MODE_PROP_IMMUTABLE))
  3649. return drm_atomic_get_property(obj, property, val);
  3650. for (i = 0; i < obj->properties->count; i++) {
  3651. if (obj->properties->properties[i] == property) {
  3652. *val = obj->properties->values[i];
  3653. return 0;
  3654. }
  3655. }
  3656. return -EINVAL;
  3657. }
  3658. EXPORT_SYMBOL(drm_object_property_get_value);
  3659. /**
  3660. * drm_mode_getproperty_ioctl - get the property metadata
  3661. * @dev: DRM device
  3662. * @data: ioctl data
  3663. * @file_priv: DRM file info
  3664. *
  3665. * This function retrieves the metadata for a given property, like the different
  3666. * possible values for an enum property or the limits for a range property.
  3667. *
  3668. * Blob properties are special
  3669. *
  3670. * Called by the user via ioctl.
  3671. *
  3672. * Returns:
  3673. * Zero on success, negative errno on failure.
  3674. */
  3675. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3676. void *data, struct drm_file *file_priv)
  3677. {
  3678. struct drm_mode_get_property *out_resp = data;
  3679. struct drm_property *property;
  3680. int enum_count = 0;
  3681. int value_count = 0;
  3682. int ret = 0, i;
  3683. int copied;
  3684. struct drm_property_enum *prop_enum;
  3685. struct drm_mode_property_enum __user *enum_ptr;
  3686. uint64_t __user *values_ptr;
  3687. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3688. return -EINVAL;
  3689. drm_modeset_lock_all(dev);
  3690. property = drm_property_find(dev, out_resp->prop_id);
  3691. if (!property) {
  3692. ret = -ENOENT;
  3693. goto done;
  3694. }
  3695. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3696. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3697. list_for_each_entry(prop_enum, &property->enum_list, head)
  3698. enum_count++;
  3699. }
  3700. value_count = property->num_values;
  3701. strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3702. out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3703. out_resp->flags = property->flags;
  3704. if ((out_resp->count_values >= value_count) && value_count) {
  3705. values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3706. for (i = 0; i < value_count; i++) {
  3707. if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3708. ret = -EFAULT;
  3709. goto done;
  3710. }
  3711. }
  3712. }
  3713. out_resp->count_values = value_count;
  3714. if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3715. drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3716. if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3717. copied = 0;
  3718. enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3719. list_for_each_entry(prop_enum, &property->enum_list, head) {
  3720. if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  3721. ret = -EFAULT;
  3722. goto done;
  3723. }
  3724. if (copy_to_user(&enum_ptr[copied].name,
  3725. &prop_enum->name, DRM_PROP_NAME_LEN)) {
  3726. ret = -EFAULT;
  3727. goto done;
  3728. }
  3729. copied++;
  3730. }
  3731. }
  3732. out_resp->count_enum_blobs = enum_count;
  3733. }
  3734. /*
  3735. * NOTE: The idea seems to have been to use this to read all the blob
  3736. * property values. But nothing ever added them to the corresponding
  3737. * list, userspace always used the special-purpose get_blob ioctl to
  3738. * read the value for a blob property. It also doesn't make a lot of
  3739. * sense to return values here when everything else is just metadata for
  3740. * the property itself.
  3741. */
  3742. if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  3743. out_resp->count_enum_blobs = 0;
  3744. done:
  3745. drm_modeset_unlock_all(dev);
  3746. return ret;
  3747. }
  3748. static void drm_property_free_blob(struct kref *kref)
  3749. {
  3750. struct drm_property_blob *blob =
  3751. container_of(kref, struct drm_property_blob, base.refcount);
  3752. mutex_lock(&blob->dev->mode_config.blob_lock);
  3753. list_del(&blob->head_global);
  3754. mutex_unlock(&blob->dev->mode_config.blob_lock);
  3755. drm_mode_object_unregister(blob->dev, &blob->base);
  3756. kfree(blob);
  3757. }
  3758. /**
  3759. * drm_property_create_blob - Create new blob property
  3760. *
  3761. * Creates a new blob property for a specified DRM device, optionally
  3762. * copying data.
  3763. *
  3764. * @dev: DRM device to create property for
  3765. * @length: Length to allocate for blob data
  3766. * @data: If specified, copies data into blob
  3767. *
  3768. * Returns:
  3769. * New blob property with a single reference on success, or an ERR_PTR
  3770. * value on failure.
  3771. */
  3772. struct drm_property_blob *
  3773. drm_property_create_blob(struct drm_device *dev, size_t length,
  3774. const void *data)
  3775. {
  3776. struct drm_property_blob *blob;
  3777. int ret;
  3778. if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
  3779. return ERR_PTR(-EINVAL);
  3780. blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  3781. if (!blob)
  3782. return ERR_PTR(-ENOMEM);
  3783. /* This must be explicitly initialised, so we can safely call list_del
  3784. * on it in the removal handler, even if it isn't in a file list. */
  3785. INIT_LIST_HEAD(&blob->head_file);
  3786. blob->length = length;
  3787. blob->dev = dev;
  3788. if (data)
  3789. memcpy(blob->data, data, length);
  3790. ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
  3791. true, drm_property_free_blob);
  3792. if (ret) {
  3793. kfree(blob);
  3794. return ERR_PTR(-EINVAL);
  3795. }
  3796. mutex_lock(&dev->mode_config.blob_lock);
  3797. list_add_tail(&blob->head_global,
  3798. &dev->mode_config.property_blob_list);
  3799. mutex_unlock(&dev->mode_config.blob_lock);
  3800. return blob;
  3801. }
  3802. EXPORT_SYMBOL(drm_property_create_blob);
  3803. /**
  3804. * drm_property_unreference_blob - Unreference a blob property
  3805. *
  3806. * Drop a reference on a blob property. May free the object.
  3807. *
  3808. * @blob: Pointer to blob property
  3809. */
  3810. void drm_property_unreference_blob(struct drm_property_blob *blob)
  3811. {
  3812. if (!blob)
  3813. return;
  3814. drm_mode_object_unreference(&blob->base);
  3815. }
  3816. EXPORT_SYMBOL(drm_property_unreference_blob);
  3817. /**
  3818. * drm_property_destroy_user_blobs - destroy all blobs created by this client
  3819. * @dev: DRM device
  3820. * @file_priv: destroy all blobs owned by this file handle
  3821. */
  3822. void drm_property_destroy_user_blobs(struct drm_device *dev,
  3823. struct drm_file *file_priv)
  3824. {
  3825. struct drm_property_blob *blob, *bt;
  3826. /*
  3827. * When the file gets released that means no one else can access the
  3828. * blob list any more, so no need to grab dev->blob_lock.
  3829. */
  3830. list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
  3831. list_del_init(&blob->head_file);
  3832. drm_property_unreference_blob(blob);
  3833. }
  3834. }
  3835. /**
  3836. * drm_property_reference_blob - Take a reference on an existing property
  3837. *
  3838. * Take a new reference on an existing blob property.
  3839. *
  3840. * @blob: Pointer to blob property
  3841. */
  3842. struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
  3843. {
  3844. drm_mode_object_reference(&blob->base);
  3845. return blob;
  3846. }
  3847. EXPORT_SYMBOL(drm_property_reference_blob);
  3848. /**
  3849. * drm_property_lookup_blob - look up a blob property and take a reference
  3850. * @dev: drm device
  3851. * @id: id of the blob property
  3852. *
  3853. * If successful, this takes an additional reference to the blob property.
  3854. * callers need to make sure to eventually unreference the returned property
  3855. * again, using @drm_property_unreference_blob.
  3856. */
  3857. struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
  3858. uint32_t id)
  3859. {
  3860. struct drm_mode_object *obj;
  3861. struct drm_property_blob *blob = NULL;
  3862. obj = _object_find(dev, id, DRM_MODE_OBJECT_BLOB);
  3863. if (obj)
  3864. blob = obj_to_blob(obj);
  3865. return blob;
  3866. }
  3867. EXPORT_SYMBOL(drm_property_lookup_blob);
  3868. /**
  3869. * drm_property_replace_global_blob - atomically replace existing blob property
  3870. * @dev: drm device
  3871. * @replace: location of blob property pointer to be replaced
  3872. * @length: length of data for new blob, or 0 for no data
  3873. * @data: content for new blob, or NULL for no data
  3874. * @obj_holds_id: optional object for property holding blob ID
  3875. * @prop_holds_id: optional property holding blob ID
  3876. * @return 0 on success or error on failure
  3877. *
  3878. * This function will atomically replace a global property in the blob list,
  3879. * optionally updating a property which holds the ID of that property. It is
  3880. * guaranteed to be atomic: no caller will be allowed to see intermediate
  3881. * results, and either the entire operation will succeed and clean up the
  3882. * previous property, or it will fail and the state will be unchanged.
  3883. *
  3884. * If length is 0 or data is NULL, no new blob will be created, and the holding
  3885. * property, if specified, will be set to 0.
  3886. *
  3887. * Access to the replace pointer is assumed to be protected by the caller, e.g.
  3888. * by holding the relevant modesetting object lock for its parent.
  3889. *
  3890. * For example, a drm_connector has a 'PATH' property, which contains the ID
  3891. * of a blob property with the value of the MST path information. Calling this
  3892. * function with replace pointing to the connector's path_blob_ptr, length and
  3893. * data set for the new path information, obj_holds_id set to the connector's
  3894. * base object, and prop_holds_id set to the path property name, will perform
  3895. * a completely atomic update. The access to path_blob_ptr is protected by the
  3896. * caller holding a lock on the connector.
  3897. */
  3898. static int drm_property_replace_global_blob(struct drm_device *dev,
  3899. struct drm_property_blob **replace,
  3900. size_t length,
  3901. const void *data,
  3902. struct drm_mode_object *obj_holds_id,
  3903. struct drm_property *prop_holds_id)
  3904. {
  3905. struct drm_property_blob *new_blob = NULL;
  3906. struct drm_property_blob *old_blob = NULL;
  3907. int ret;
  3908. WARN_ON(replace == NULL);
  3909. old_blob = *replace;
  3910. if (length && data) {
  3911. new_blob = drm_property_create_blob(dev, length, data);
  3912. if (IS_ERR(new_blob))
  3913. return PTR_ERR(new_blob);
  3914. }
  3915. /* This does not need to be synchronised with blob_lock, as the
  3916. * get_properties ioctl locks all modesetting objects, and
  3917. * obj_holds_id must be locked before calling here, so we cannot
  3918. * have its value out of sync with the list membership modified
  3919. * below under blob_lock. */
  3920. if (obj_holds_id) {
  3921. ret = drm_object_property_set_value(obj_holds_id,
  3922. prop_holds_id,
  3923. new_blob ?
  3924. new_blob->base.id : 0);
  3925. if (ret != 0)
  3926. goto err_created;
  3927. }
  3928. drm_property_unreference_blob(old_blob);
  3929. *replace = new_blob;
  3930. return 0;
  3931. err_created:
  3932. drm_property_unreference_blob(new_blob);
  3933. return ret;
  3934. }
  3935. /**
  3936. * drm_mode_getblob_ioctl - get the contents of a blob property value
  3937. * @dev: DRM device
  3938. * @data: ioctl data
  3939. * @file_priv: DRM file info
  3940. *
  3941. * This function retrieves the contents of a blob property. The value stored in
  3942. * an object's blob property is just a normal modeset object id.
  3943. *
  3944. * Called by the user via ioctl.
  3945. *
  3946. * Returns:
  3947. * Zero on success, negative errno on failure.
  3948. */
  3949. int drm_mode_getblob_ioctl(struct drm_device *dev,
  3950. void *data, struct drm_file *file_priv)
  3951. {
  3952. struct drm_mode_get_blob *out_resp = data;
  3953. struct drm_property_blob *blob;
  3954. int ret = 0;
  3955. void __user *blob_ptr;
  3956. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3957. return -EINVAL;
  3958. blob = drm_property_lookup_blob(dev, out_resp->blob_id);
  3959. if (!blob)
  3960. return -ENOENT;
  3961. if (out_resp->length == blob->length) {
  3962. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3963. if (copy_to_user(blob_ptr, blob->data, blob->length)) {
  3964. ret = -EFAULT;
  3965. goto unref;
  3966. }
  3967. }
  3968. out_resp->length = blob->length;
  3969. unref:
  3970. drm_property_unreference_blob(blob);
  3971. return ret;
  3972. }
  3973. /**
  3974. * drm_mode_createblob_ioctl - create a new blob property
  3975. * @dev: DRM device
  3976. * @data: ioctl data
  3977. * @file_priv: DRM file info
  3978. *
  3979. * This function creates a new blob property with user-defined values. In order
  3980. * to give us sensible validation and checking when creating, rather than at
  3981. * every potential use, we also require a type to be provided upfront.
  3982. *
  3983. * Called by the user via ioctl.
  3984. *
  3985. * Returns:
  3986. * Zero on success, negative errno on failure.
  3987. */
  3988. int drm_mode_createblob_ioctl(struct drm_device *dev,
  3989. void *data, struct drm_file *file_priv)
  3990. {
  3991. struct drm_mode_create_blob *out_resp = data;
  3992. struct drm_property_blob *blob;
  3993. void __user *blob_ptr;
  3994. int ret = 0;
  3995. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3996. return -EINVAL;
  3997. blob = drm_property_create_blob(dev, out_resp->length, NULL);
  3998. if (IS_ERR(blob))
  3999. return PTR_ERR(blob);
  4000. blob_ptr = (void __user *)(unsigned long)out_resp->data;
  4001. if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
  4002. ret = -EFAULT;
  4003. goto out_blob;
  4004. }
  4005. /* Dropping the lock between create_blob and our access here is safe
  4006. * as only the same file_priv can remove the blob; at this point, it is
  4007. * not associated with any file_priv. */
  4008. mutex_lock(&dev->mode_config.blob_lock);
  4009. out_resp->blob_id = blob->base.id;
  4010. list_add_tail(&blob->head_file, &file_priv->blobs);
  4011. mutex_unlock(&dev->mode_config.blob_lock);
  4012. return 0;
  4013. out_blob:
  4014. drm_property_unreference_blob(blob);
  4015. return ret;
  4016. }
  4017. /**
  4018. * drm_mode_destroyblob_ioctl - destroy a user blob property
  4019. * @dev: DRM device
  4020. * @data: ioctl data
  4021. * @file_priv: DRM file info
  4022. *
  4023. * Destroy an existing user-defined blob property.
  4024. *
  4025. * Called by the user via ioctl.
  4026. *
  4027. * Returns:
  4028. * Zero on success, negative errno on failure.
  4029. */
  4030. int drm_mode_destroyblob_ioctl(struct drm_device *dev,
  4031. void *data, struct drm_file *file_priv)
  4032. {
  4033. struct drm_mode_destroy_blob *out_resp = data;
  4034. struct drm_property_blob *blob = NULL, *bt;
  4035. bool found = false;
  4036. int ret = 0;
  4037. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4038. return -EINVAL;
  4039. blob = drm_property_lookup_blob(dev, out_resp->blob_id);
  4040. if (!blob)
  4041. return -ENOENT;
  4042. mutex_lock(&dev->mode_config.blob_lock);
  4043. /* Ensure the property was actually created by this user. */
  4044. list_for_each_entry(bt, &file_priv->blobs, head_file) {
  4045. if (bt == blob) {
  4046. found = true;
  4047. break;
  4048. }
  4049. }
  4050. if (!found) {
  4051. ret = -EPERM;
  4052. goto err;
  4053. }
  4054. /* We must drop head_file here, because we may not be the last
  4055. * reference on the blob. */
  4056. list_del_init(&blob->head_file);
  4057. mutex_unlock(&dev->mode_config.blob_lock);
  4058. /* One reference from lookup, and one from the filp. */
  4059. drm_property_unreference_blob(blob);
  4060. drm_property_unreference_blob(blob);
  4061. return 0;
  4062. err:
  4063. mutex_unlock(&dev->mode_config.blob_lock);
  4064. drm_property_unreference_blob(blob);
  4065. return ret;
  4066. }
  4067. /**
  4068. * drm_mode_connector_set_path_property - set tile property on connector
  4069. * @connector: connector to set property on.
  4070. * @path: path to use for property; must not be NULL.
  4071. *
  4072. * This creates a property to expose to userspace to specify a
  4073. * connector path. This is mainly used for DisplayPort MST where
  4074. * connectors have a topology and we want to allow userspace to give
  4075. * them more meaningful names.
  4076. *
  4077. * Returns:
  4078. * Zero on success, negative errno on failure.
  4079. */
  4080. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  4081. const char *path)
  4082. {
  4083. struct drm_device *dev = connector->dev;
  4084. int ret;
  4085. ret = drm_property_replace_global_blob(dev,
  4086. &connector->path_blob_ptr,
  4087. strlen(path) + 1,
  4088. path,
  4089. &connector->base,
  4090. dev->mode_config.path_property);
  4091. return ret;
  4092. }
  4093. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  4094. /**
  4095. * drm_mode_connector_set_tile_property - set tile property on connector
  4096. * @connector: connector to set property on.
  4097. *
  4098. * This looks up the tile information for a connector, and creates a
  4099. * property for userspace to parse if it exists. The property is of
  4100. * the form of 8 integers using ':' as a separator.
  4101. *
  4102. * Returns:
  4103. * Zero on success, errno on failure.
  4104. */
  4105. int drm_mode_connector_set_tile_property(struct drm_connector *connector)
  4106. {
  4107. struct drm_device *dev = connector->dev;
  4108. char tile[256];
  4109. int ret;
  4110. if (!connector->has_tile) {
  4111. ret = drm_property_replace_global_blob(dev,
  4112. &connector->tile_blob_ptr,
  4113. 0,
  4114. NULL,
  4115. &connector->base,
  4116. dev->mode_config.tile_property);
  4117. return ret;
  4118. }
  4119. snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
  4120. connector->tile_group->id, connector->tile_is_single_monitor,
  4121. connector->num_h_tile, connector->num_v_tile,
  4122. connector->tile_h_loc, connector->tile_v_loc,
  4123. connector->tile_h_size, connector->tile_v_size);
  4124. ret = drm_property_replace_global_blob(dev,
  4125. &connector->tile_blob_ptr,
  4126. strlen(tile) + 1,
  4127. tile,
  4128. &connector->base,
  4129. dev->mode_config.tile_property);
  4130. return ret;
  4131. }
  4132. EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
  4133. /**
  4134. * drm_mode_connector_update_edid_property - update the edid property of a connector
  4135. * @connector: drm connector
  4136. * @edid: new value of the edid property
  4137. *
  4138. * This function creates a new blob modeset object and assigns its id to the
  4139. * connector's edid property.
  4140. *
  4141. * Returns:
  4142. * Zero on success, negative errno on failure.
  4143. */
  4144. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  4145. const struct edid *edid)
  4146. {
  4147. struct drm_device *dev = connector->dev;
  4148. size_t size = 0;
  4149. int ret;
  4150. /* ignore requests to set edid when overridden */
  4151. if (connector->override_edid)
  4152. return 0;
  4153. if (edid)
  4154. size = EDID_LENGTH * (1 + edid->extensions);
  4155. ret = drm_property_replace_global_blob(dev,
  4156. &connector->edid_blob_ptr,
  4157. size,
  4158. edid,
  4159. &connector->base,
  4160. dev->mode_config.edid_property);
  4161. return ret;
  4162. }
  4163. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  4164. /* Some properties could refer to dynamic refcnt'd objects, or things that
  4165. * need special locking to handle lifetime issues (ie. to ensure the prop
  4166. * value doesn't become invalid part way through the property update due to
  4167. * race). The value returned by reference via 'obj' should be passed back
  4168. * to drm_property_change_valid_put() after the property is set (and the
  4169. * object to which the property is attached has a chance to take it's own
  4170. * reference).
  4171. */
  4172. bool drm_property_change_valid_get(struct drm_property *property,
  4173. uint64_t value, struct drm_mode_object **ref)
  4174. {
  4175. int i;
  4176. if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  4177. return false;
  4178. *ref = NULL;
  4179. if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
  4180. if (value < property->values[0] || value > property->values[1])
  4181. return false;
  4182. return true;
  4183. } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
  4184. int64_t svalue = U642I64(value);
  4185. if (svalue < U642I64(property->values[0]) ||
  4186. svalue > U642I64(property->values[1]))
  4187. return false;
  4188. return true;
  4189. } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  4190. uint64_t valid_mask = 0;
  4191. for (i = 0; i < property->num_values; i++)
  4192. valid_mask |= (1ULL << property->values[i]);
  4193. return !(value & ~valid_mask);
  4194. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  4195. struct drm_property_blob *blob;
  4196. if (value == 0)
  4197. return true;
  4198. blob = drm_property_lookup_blob(property->dev, value);
  4199. if (blob) {
  4200. *ref = &blob->base;
  4201. return true;
  4202. } else {
  4203. return false;
  4204. }
  4205. } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4206. /* a zero value for an object property translates to null: */
  4207. if (value == 0)
  4208. return true;
  4209. return _object_find(property->dev, value, property->values[0]) != NULL;
  4210. }
  4211. for (i = 0; i < property->num_values; i++)
  4212. if (property->values[i] == value)
  4213. return true;
  4214. return false;
  4215. }
  4216. void drm_property_change_valid_put(struct drm_property *property,
  4217. struct drm_mode_object *ref)
  4218. {
  4219. if (!ref)
  4220. return;
  4221. if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
  4222. drm_mode_object_unreference(ref);
  4223. } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
  4224. drm_property_unreference_blob(obj_to_blob(ref));
  4225. }
  4226. /**
  4227. * drm_mode_connector_property_set_ioctl - set the current value of a connector property
  4228. * @dev: DRM device
  4229. * @data: ioctl data
  4230. * @file_priv: DRM file info
  4231. *
  4232. * This function sets the current value for a connectors's property. It also
  4233. * calls into a driver's ->set_property callback to update the hardware state
  4234. *
  4235. * Called by the user via ioctl.
  4236. *
  4237. * Returns:
  4238. * Zero on success, negative errno on failure.
  4239. */
  4240. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  4241. void *data, struct drm_file *file_priv)
  4242. {
  4243. struct drm_mode_connector_set_property *conn_set_prop = data;
  4244. struct drm_mode_obj_set_property obj_set_prop = {
  4245. .value = conn_set_prop->value,
  4246. .prop_id = conn_set_prop->prop_id,
  4247. .obj_id = conn_set_prop->connector_id,
  4248. .obj_type = DRM_MODE_OBJECT_CONNECTOR
  4249. };
  4250. /* It does all the locking and checking we need */
  4251. return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
  4252. }
  4253. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  4254. struct drm_property *property,
  4255. uint64_t value)
  4256. {
  4257. int ret = -EINVAL;
  4258. struct drm_connector *connector = obj_to_connector(obj);
  4259. /* Do DPMS ourselves */
  4260. if (property == connector->dev->mode_config.dpms_property) {
  4261. ret = (*connector->funcs->dpms)(connector, (int)value);
  4262. } else if (connector->funcs->set_property)
  4263. ret = connector->funcs->set_property(connector, property, value);
  4264. /* store the property value if successful */
  4265. if (!ret)
  4266. drm_object_property_set_value(&connector->base, property, value);
  4267. return ret;
  4268. }
  4269. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  4270. struct drm_property *property,
  4271. uint64_t value)
  4272. {
  4273. int ret = -EINVAL;
  4274. struct drm_crtc *crtc = obj_to_crtc(obj);
  4275. if (crtc->funcs->set_property)
  4276. ret = crtc->funcs->set_property(crtc, property, value);
  4277. if (!ret)
  4278. drm_object_property_set_value(obj, property, value);
  4279. return ret;
  4280. }
  4281. /**
  4282. * drm_mode_plane_set_obj_prop - set the value of a property
  4283. * @plane: drm plane object to set property value for
  4284. * @property: property to set
  4285. * @value: value the property should be set to
  4286. *
  4287. * This functions sets a given property on a given plane object. This function
  4288. * calls the driver's ->set_property callback and changes the software state of
  4289. * the property if the callback succeeds.
  4290. *
  4291. * Returns:
  4292. * Zero on success, error code on failure.
  4293. */
  4294. int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
  4295. struct drm_property *property,
  4296. uint64_t value)
  4297. {
  4298. int ret = -EINVAL;
  4299. struct drm_mode_object *obj = &plane->base;
  4300. if (plane->funcs->set_property)
  4301. ret = plane->funcs->set_property(plane, property, value);
  4302. if (!ret)
  4303. drm_object_property_set_value(obj, property, value);
  4304. return ret;
  4305. }
  4306. EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
  4307. /**
  4308. * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
  4309. * @dev: DRM device
  4310. * @data: ioctl data
  4311. * @file_priv: DRM file info
  4312. *
  4313. * This function retrieves the current value for an object's property. Compared
  4314. * to the connector specific ioctl this one is extended to also work on crtc and
  4315. * plane objects.
  4316. *
  4317. * Called by the user via ioctl.
  4318. *
  4319. * Returns:
  4320. * Zero on success, negative errno on failure.
  4321. */
  4322. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  4323. struct drm_file *file_priv)
  4324. {
  4325. struct drm_mode_obj_get_properties *arg = data;
  4326. struct drm_mode_object *obj;
  4327. int ret = 0;
  4328. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4329. return -EINVAL;
  4330. drm_modeset_lock_all(dev);
  4331. obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4332. if (!obj) {
  4333. ret = -ENOENT;
  4334. goto out;
  4335. }
  4336. if (!obj->properties) {
  4337. ret = -EINVAL;
  4338. goto out_unref;
  4339. }
  4340. ret = get_properties(obj, file_priv->atomic,
  4341. (uint32_t __user *)(unsigned long)(arg->props_ptr),
  4342. (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
  4343. &arg->count_props);
  4344. out_unref:
  4345. drm_mode_object_unreference(obj);
  4346. out:
  4347. drm_modeset_unlock_all(dev);
  4348. return ret;
  4349. }
  4350. /**
  4351. * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  4352. * @dev: DRM device
  4353. * @data: ioctl data
  4354. * @file_priv: DRM file info
  4355. *
  4356. * This function sets the current value for an object's property. It also calls
  4357. * into a driver's ->set_property callback to update the hardware state.
  4358. * Compared to the connector specific ioctl this one is extended to also work on
  4359. * crtc and plane objects.
  4360. *
  4361. * Called by the user via ioctl.
  4362. *
  4363. * Returns:
  4364. * Zero on success, negative errno on failure.
  4365. */
  4366. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  4367. struct drm_file *file_priv)
  4368. {
  4369. struct drm_mode_obj_set_property *arg = data;
  4370. struct drm_mode_object *arg_obj;
  4371. struct drm_mode_object *prop_obj;
  4372. struct drm_property *property;
  4373. int i, ret = -EINVAL;
  4374. struct drm_mode_object *ref;
  4375. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4376. return -EINVAL;
  4377. drm_modeset_lock_all(dev);
  4378. arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4379. if (!arg_obj) {
  4380. ret = -ENOENT;
  4381. goto out;
  4382. }
  4383. if (!arg_obj->properties)
  4384. goto out_unref;
  4385. for (i = 0; i < arg_obj->properties->count; i++)
  4386. if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
  4387. break;
  4388. if (i == arg_obj->properties->count)
  4389. goto out_unref;
  4390. prop_obj = drm_mode_object_find(dev, arg->prop_id,
  4391. DRM_MODE_OBJECT_PROPERTY);
  4392. if (!prop_obj) {
  4393. ret = -ENOENT;
  4394. goto out_unref;
  4395. }
  4396. property = obj_to_property(prop_obj);
  4397. if (!drm_property_change_valid_get(property, arg->value, &ref))
  4398. goto out_unref;
  4399. switch (arg_obj->type) {
  4400. case DRM_MODE_OBJECT_CONNECTOR:
  4401. ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  4402. arg->value);
  4403. break;
  4404. case DRM_MODE_OBJECT_CRTC:
  4405. ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  4406. break;
  4407. case DRM_MODE_OBJECT_PLANE:
  4408. ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
  4409. property, arg->value);
  4410. break;
  4411. }
  4412. drm_property_change_valid_put(property, ref);
  4413. out_unref:
  4414. drm_mode_object_unreference(arg_obj);
  4415. out:
  4416. drm_modeset_unlock_all(dev);
  4417. return ret;
  4418. }
  4419. /**
  4420. * drm_mode_connector_attach_encoder - attach a connector to an encoder
  4421. * @connector: connector to attach
  4422. * @encoder: encoder to attach @connector to
  4423. *
  4424. * This function links up a connector to an encoder. Note that the routing
  4425. * restrictions between encoders and crtcs are exposed to userspace through the
  4426. * possible_clones and possible_crtcs bitmasks.
  4427. *
  4428. * Returns:
  4429. * Zero on success, negative errno on failure.
  4430. */
  4431. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  4432. struct drm_encoder *encoder)
  4433. {
  4434. int i;
  4435. /*
  4436. * In the past, drivers have attempted to model the static association
  4437. * of connector to encoder in simple connector/encoder devices using a
  4438. * direct assignment of connector->encoder = encoder. This connection
  4439. * is a logical one and the responsibility of the core, so drivers are
  4440. * expected not to mess with this.
  4441. *
  4442. * Note that the error return should've been enough here, but a large
  4443. * majority of drivers ignores the return value, so add in a big WARN
  4444. * to get people's attention.
  4445. */
  4446. if (WARN_ON(connector->encoder))
  4447. return -EINVAL;
  4448. for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4449. if (connector->encoder_ids[i] == 0) {
  4450. connector->encoder_ids[i] = encoder->base.id;
  4451. return 0;
  4452. }
  4453. }
  4454. return -ENOMEM;
  4455. }
  4456. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  4457. /**
  4458. * drm_mode_crtc_set_gamma_size - set the gamma table size
  4459. * @crtc: CRTC to set the gamma table size for
  4460. * @gamma_size: size of the gamma table
  4461. *
  4462. * Drivers which support gamma tables should set this to the supported gamma
  4463. * table size when initializing the CRTC. Currently the drm core only supports a
  4464. * fixed gamma table size.
  4465. *
  4466. * Returns:
  4467. * Zero on success, negative errno on failure.
  4468. */
  4469. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  4470. int gamma_size)
  4471. {
  4472. uint16_t *r_base, *g_base, *b_base;
  4473. int i;
  4474. crtc->gamma_size = gamma_size;
  4475. crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
  4476. GFP_KERNEL);
  4477. if (!crtc->gamma_store) {
  4478. crtc->gamma_size = 0;
  4479. return -ENOMEM;
  4480. }
  4481. r_base = crtc->gamma_store;
  4482. g_base = r_base + gamma_size;
  4483. b_base = g_base + gamma_size;
  4484. for (i = 0; i < gamma_size; i++) {
  4485. r_base[i] = i << 8;
  4486. g_base[i] = i << 8;
  4487. b_base[i] = i << 8;
  4488. }
  4489. return 0;
  4490. }
  4491. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  4492. /**
  4493. * drm_mode_gamma_set_ioctl - set the gamma table
  4494. * @dev: DRM device
  4495. * @data: ioctl data
  4496. * @file_priv: DRM file info
  4497. *
  4498. * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  4499. * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  4500. *
  4501. * Called by the user via ioctl.
  4502. *
  4503. * Returns:
  4504. * Zero on success, negative errno on failure.
  4505. */
  4506. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  4507. void *data, struct drm_file *file_priv)
  4508. {
  4509. struct drm_mode_crtc_lut *crtc_lut = data;
  4510. struct drm_crtc *crtc;
  4511. void *r_base, *g_base, *b_base;
  4512. int size;
  4513. int ret = 0;
  4514. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4515. return -EINVAL;
  4516. drm_modeset_lock_all(dev);
  4517. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4518. if (!crtc) {
  4519. ret = -ENOENT;
  4520. goto out;
  4521. }
  4522. if (crtc->funcs->gamma_set == NULL) {
  4523. ret = -ENOSYS;
  4524. goto out;
  4525. }
  4526. /* memcpy into gamma store */
  4527. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4528. ret = -EINVAL;
  4529. goto out;
  4530. }
  4531. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4532. r_base = crtc->gamma_store;
  4533. if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  4534. ret = -EFAULT;
  4535. goto out;
  4536. }
  4537. g_base = r_base + size;
  4538. if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  4539. ret = -EFAULT;
  4540. goto out;
  4541. }
  4542. b_base = g_base + size;
  4543. if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  4544. ret = -EFAULT;
  4545. goto out;
  4546. }
  4547. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  4548. out:
  4549. drm_modeset_unlock_all(dev);
  4550. return ret;
  4551. }
  4552. /**
  4553. * drm_mode_gamma_get_ioctl - get the gamma table
  4554. * @dev: DRM device
  4555. * @data: ioctl data
  4556. * @file_priv: DRM file info
  4557. *
  4558. * Copy the current gamma table into the storage provided. This also provides
  4559. * the gamma table size the driver expects, which can be used to size the
  4560. * allocated storage.
  4561. *
  4562. * Called by the user via ioctl.
  4563. *
  4564. * Returns:
  4565. * Zero on success, negative errno on failure.
  4566. */
  4567. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  4568. void *data, struct drm_file *file_priv)
  4569. {
  4570. struct drm_mode_crtc_lut *crtc_lut = data;
  4571. struct drm_crtc *crtc;
  4572. void *r_base, *g_base, *b_base;
  4573. int size;
  4574. int ret = 0;
  4575. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4576. return -EINVAL;
  4577. drm_modeset_lock_all(dev);
  4578. crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4579. if (!crtc) {
  4580. ret = -ENOENT;
  4581. goto out;
  4582. }
  4583. /* memcpy into gamma store */
  4584. if (crtc_lut->gamma_size != crtc->gamma_size) {
  4585. ret = -EINVAL;
  4586. goto out;
  4587. }
  4588. size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4589. r_base = crtc->gamma_store;
  4590. if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  4591. ret = -EFAULT;
  4592. goto out;
  4593. }
  4594. g_base = r_base + size;
  4595. if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  4596. ret = -EFAULT;
  4597. goto out;
  4598. }
  4599. b_base = g_base + size;
  4600. if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  4601. ret = -EFAULT;
  4602. goto out;
  4603. }
  4604. out:
  4605. drm_modeset_unlock_all(dev);
  4606. return ret;
  4607. }
  4608. /**
  4609. * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
  4610. * @dev: DRM device
  4611. * @data: ioctl data
  4612. * @file_priv: DRM file info
  4613. *
  4614. * This schedules an asynchronous update on a given CRTC, called page flip.
  4615. * Optionally a drm event is generated to signal the completion of the event.
  4616. * Generic drivers cannot assume that a pageflip with changed framebuffer
  4617. * properties (including driver specific metadata like tiling layout) will work,
  4618. * but some drivers support e.g. pixel format changes through the pageflip
  4619. * ioctl.
  4620. *
  4621. * Called by the user via ioctl.
  4622. *
  4623. * Returns:
  4624. * Zero on success, negative errno on failure.
  4625. */
  4626. int drm_mode_page_flip_ioctl(struct drm_device *dev,
  4627. void *data, struct drm_file *file_priv)
  4628. {
  4629. struct drm_mode_crtc_page_flip *page_flip = data;
  4630. struct drm_crtc *crtc;
  4631. struct drm_framebuffer *fb = NULL;
  4632. struct drm_pending_vblank_event *e = NULL;
  4633. int ret = -EINVAL;
  4634. if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
  4635. page_flip->reserved != 0)
  4636. return -EINVAL;
  4637. if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
  4638. return -EINVAL;
  4639. crtc = drm_crtc_find(dev, page_flip->crtc_id);
  4640. if (!crtc)
  4641. return -ENOENT;
  4642. drm_modeset_lock_crtc(crtc, crtc->primary);
  4643. if (crtc->primary->fb == NULL) {
  4644. /* The framebuffer is currently unbound, presumably
  4645. * due to a hotplug event, that userspace has not
  4646. * yet discovered.
  4647. */
  4648. ret = -EBUSY;
  4649. goto out;
  4650. }
  4651. if (crtc->funcs->page_flip == NULL)
  4652. goto out;
  4653. fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
  4654. if (!fb) {
  4655. ret = -ENOENT;
  4656. goto out;
  4657. }
  4658. if (crtc->state) {
  4659. const struct drm_plane_state *state = crtc->primary->state;
  4660. ret = check_src_coords(state->src_x, state->src_y,
  4661. state->src_w, state->src_h, fb);
  4662. } else {
  4663. ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
  4664. }
  4665. if (ret)
  4666. goto out;
  4667. if (crtc->primary->fb->pixel_format != fb->pixel_format) {
  4668. DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
  4669. ret = -EINVAL;
  4670. goto out;
  4671. }
  4672. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
  4673. e = kzalloc(sizeof *e, GFP_KERNEL);
  4674. if (!e) {
  4675. ret = -ENOMEM;
  4676. goto out;
  4677. }
  4678. e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
  4679. e->event.base.length = sizeof(e->event);
  4680. e->event.user_data = page_flip->user_data;
  4681. ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
  4682. if (ret) {
  4683. kfree(e);
  4684. goto out;
  4685. }
  4686. }
  4687. crtc->primary->old_fb = crtc->primary->fb;
  4688. ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
  4689. if (ret) {
  4690. if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
  4691. drm_event_cancel_free(dev, &e->base);
  4692. /* Keep the old fb, don't unref it. */
  4693. crtc->primary->old_fb = NULL;
  4694. } else {
  4695. crtc->primary->fb = fb;
  4696. /* Unref only the old framebuffer. */
  4697. fb = NULL;
  4698. }
  4699. out:
  4700. if (fb)
  4701. drm_framebuffer_unreference(fb);
  4702. if (crtc->primary->old_fb)
  4703. drm_framebuffer_unreference(crtc->primary->old_fb);
  4704. crtc->primary->old_fb = NULL;
  4705. drm_modeset_unlock_crtc(crtc);
  4706. return ret;
  4707. }
  4708. /**
  4709. * drm_mode_config_reset - call ->reset callbacks
  4710. * @dev: drm device
  4711. *
  4712. * This functions calls all the crtc's, encoder's and connector's ->reset
  4713. * callback. Drivers can use this in e.g. their driver load or resume code to
  4714. * reset hardware and software state.
  4715. */
  4716. void drm_mode_config_reset(struct drm_device *dev)
  4717. {
  4718. struct drm_crtc *crtc;
  4719. struct drm_plane *plane;
  4720. struct drm_encoder *encoder;
  4721. struct drm_connector *connector;
  4722. drm_for_each_plane(plane, dev)
  4723. if (plane->funcs->reset)
  4724. plane->funcs->reset(plane);
  4725. drm_for_each_crtc(crtc, dev)
  4726. if (crtc->funcs->reset)
  4727. crtc->funcs->reset(crtc);
  4728. drm_for_each_encoder(encoder, dev)
  4729. if (encoder->funcs->reset)
  4730. encoder->funcs->reset(encoder);
  4731. mutex_lock(&dev->mode_config.mutex);
  4732. drm_for_each_connector(connector, dev)
  4733. if (connector->funcs->reset)
  4734. connector->funcs->reset(connector);
  4735. mutex_unlock(&dev->mode_config.mutex);
  4736. }
  4737. EXPORT_SYMBOL(drm_mode_config_reset);
  4738. /**
  4739. * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
  4740. * @dev: DRM device
  4741. * @data: ioctl data
  4742. * @file_priv: DRM file info
  4743. *
  4744. * This creates a new dumb buffer in the driver's backing storage manager (GEM,
  4745. * TTM or something else entirely) and returns the resulting buffer handle. This
  4746. * handle can then be wrapped up into a framebuffer modeset object.
  4747. *
  4748. * Note that userspace is not allowed to use such objects for render
  4749. * acceleration - drivers must create their own private ioctls for such a use
  4750. * case.
  4751. *
  4752. * Called by the user via ioctl.
  4753. *
  4754. * Returns:
  4755. * Zero on success, negative errno on failure.
  4756. */
  4757. int drm_mode_create_dumb_ioctl(struct drm_device *dev,
  4758. void *data, struct drm_file *file_priv)
  4759. {
  4760. struct drm_mode_create_dumb *args = data;
  4761. u32 cpp, stride, size;
  4762. if (!dev->driver->dumb_create)
  4763. return -ENOSYS;
  4764. if (!args->width || !args->height || !args->bpp)
  4765. return -EINVAL;
  4766. /* overflow checks for 32bit size calculations */
  4767. /* NOTE: DIV_ROUND_UP() can overflow */
  4768. cpp = DIV_ROUND_UP(args->bpp, 8);
  4769. if (!cpp || cpp > 0xffffffffU / args->width)
  4770. return -EINVAL;
  4771. stride = cpp * args->width;
  4772. if (args->height > 0xffffffffU / stride)
  4773. return -EINVAL;
  4774. /* test for wrap-around */
  4775. size = args->height * stride;
  4776. if (PAGE_ALIGN(size) == 0)
  4777. return -EINVAL;
  4778. /*
  4779. * handle, pitch and size are output parameters. Zero them out to
  4780. * prevent drivers from accidentally using uninitialized data. Since
  4781. * not all existing userspace is clearing these fields properly we
  4782. * cannot reject IOCTL with garbage in them.
  4783. */
  4784. args->handle = 0;
  4785. args->pitch = 0;
  4786. args->size = 0;
  4787. return dev->driver->dumb_create(file_priv, dev, args);
  4788. }
  4789. /**
  4790. * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
  4791. * @dev: DRM device
  4792. * @data: ioctl data
  4793. * @file_priv: DRM file info
  4794. *
  4795. * Allocate an offset in the drm device node's address space to be able to
  4796. * memory map a dumb buffer.
  4797. *
  4798. * Called by the user via ioctl.
  4799. *
  4800. * Returns:
  4801. * Zero on success, negative errno on failure.
  4802. */
  4803. int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
  4804. void *data, struct drm_file *file_priv)
  4805. {
  4806. struct drm_mode_map_dumb *args = data;
  4807. /* call driver ioctl to get mmap offset */
  4808. if (!dev->driver->dumb_map_offset)
  4809. return -ENOSYS;
  4810. return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
  4811. }
  4812. /**
  4813. * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
  4814. * @dev: DRM device
  4815. * @data: ioctl data
  4816. * @file_priv: DRM file info
  4817. *
  4818. * This destroys the userspace handle for the given dumb backing storage buffer.
  4819. * Since buffer objects must be reference counted in the kernel a buffer object
  4820. * won't be immediately freed if a framebuffer modeset object still uses it.
  4821. *
  4822. * Called by the user via ioctl.
  4823. *
  4824. * Returns:
  4825. * Zero on success, negative errno on failure.
  4826. */
  4827. int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
  4828. void *data, struct drm_file *file_priv)
  4829. {
  4830. struct drm_mode_destroy_dumb *args = data;
  4831. if (!dev->driver->dumb_destroy)
  4832. return -ENOSYS;
  4833. return dev->driver->dumb_destroy(file_priv, dev, args->handle);
  4834. }
  4835. /**
  4836. * drm_fb_get_bpp_depth - get the bpp/depth values for format
  4837. * @format: pixel format (DRM_FORMAT_*)
  4838. * @depth: storage for the depth value
  4839. * @bpp: storage for the bpp value
  4840. *
  4841. * This only supports RGB formats here for compat with code that doesn't use
  4842. * pixel formats directly yet.
  4843. */
  4844. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4845. int *bpp)
  4846. {
  4847. switch (format) {
  4848. case DRM_FORMAT_C8:
  4849. case DRM_FORMAT_RGB332:
  4850. case DRM_FORMAT_BGR233:
  4851. *depth = 8;
  4852. *bpp = 8;
  4853. break;
  4854. case DRM_FORMAT_XRGB1555:
  4855. case DRM_FORMAT_XBGR1555:
  4856. case DRM_FORMAT_RGBX5551:
  4857. case DRM_FORMAT_BGRX5551:
  4858. case DRM_FORMAT_ARGB1555:
  4859. case DRM_FORMAT_ABGR1555:
  4860. case DRM_FORMAT_RGBA5551:
  4861. case DRM_FORMAT_BGRA5551:
  4862. *depth = 15;
  4863. *bpp = 16;
  4864. break;
  4865. case DRM_FORMAT_RGB565:
  4866. case DRM_FORMAT_BGR565:
  4867. *depth = 16;
  4868. *bpp = 16;
  4869. break;
  4870. case DRM_FORMAT_RGB888:
  4871. case DRM_FORMAT_BGR888:
  4872. *depth = 24;
  4873. *bpp = 24;
  4874. break;
  4875. case DRM_FORMAT_XRGB8888:
  4876. case DRM_FORMAT_XBGR8888:
  4877. case DRM_FORMAT_RGBX8888:
  4878. case DRM_FORMAT_BGRX8888:
  4879. *depth = 24;
  4880. *bpp = 32;
  4881. break;
  4882. case DRM_FORMAT_XRGB2101010:
  4883. case DRM_FORMAT_XBGR2101010:
  4884. case DRM_FORMAT_RGBX1010102:
  4885. case DRM_FORMAT_BGRX1010102:
  4886. case DRM_FORMAT_ARGB2101010:
  4887. case DRM_FORMAT_ABGR2101010:
  4888. case DRM_FORMAT_RGBA1010102:
  4889. case DRM_FORMAT_BGRA1010102:
  4890. *depth = 30;
  4891. *bpp = 32;
  4892. break;
  4893. case DRM_FORMAT_ARGB8888:
  4894. case DRM_FORMAT_ABGR8888:
  4895. case DRM_FORMAT_RGBA8888:
  4896. case DRM_FORMAT_BGRA8888:
  4897. *depth = 32;
  4898. *bpp = 32;
  4899. break;
  4900. default:
  4901. DRM_DEBUG_KMS("unsupported pixel format %s\n",
  4902. drm_get_format_name(format));
  4903. *depth = 0;
  4904. *bpp = 0;
  4905. break;
  4906. }
  4907. }
  4908. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  4909. /**
  4910. * drm_format_num_planes - get the number of planes for format
  4911. * @format: pixel format (DRM_FORMAT_*)
  4912. *
  4913. * Returns:
  4914. * The number of planes used by the specified pixel format.
  4915. */
  4916. int drm_format_num_planes(uint32_t format)
  4917. {
  4918. switch (format) {
  4919. case DRM_FORMAT_YUV410:
  4920. case DRM_FORMAT_YVU410:
  4921. case DRM_FORMAT_YUV411:
  4922. case DRM_FORMAT_YVU411:
  4923. case DRM_FORMAT_YUV420:
  4924. case DRM_FORMAT_YVU420:
  4925. case DRM_FORMAT_YUV422:
  4926. case DRM_FORMAT_YVU422:
  4927. case DRM_FORMAT_YUV444:
  4928. case DRM_FORMAT_YVU444:
  4929. return 3;
  4930. case DRM_FORMAT_NV12:
  4931. case DRM_FORMAT_NV21:
  4932. case DRM_FORMAT_NV16:
  4933. case DRM_FORMAT_NV61:
  4934. case DRM_FORMAT_NV24:
  4935. case DRM_FORMAT_NV42:
  4936. return 2;
  4937. default:
  4938. return 1;
  4939. }
  4940. }
  4941. EXPORT_SYMBOL(drm_format_num_planes);
  4942. /**
  4943. * drm_format_plane_cpp - determine the bytes per pixel value
  4944. * @format: pixel format (DRM_FORMAT_*)
  4945. * @plane: plane index
  4946. *
  4947. * Returns:
  4948. * The bytes per pixel value for the specified plane.
  4949. */
  4950. int drm_format_plane_cpp(uint32_t format, int plane)
  4951. {
  4952. unsigned int depth;
  4953. int bpp;
  4954. if (plane >= drm_format_num_planes(format))
  4955. return 0;
  4956. switch (format) {
  4957. case DRM_FORMAT_YUYV:
  4958. case DRM_FORMAT_YVYU:
  4959. case DRM_FORMAT_UYVY:
  4960. case DRM_FORMAT_VYUY:
  4961. return 2;
  4962. case DRM_FORMAT_NV12:
  4963. case DRM_FORMAT_NV21:
  4964. case DRM_FORMAT_NV16:
  4965. case DRM_FORMAT_NV61:
  4966. case DRM_FORMAT_NV24:
  4967. case DRM_FORMAT_NV42:
  4968. return plane ? 2 : 1;
  4969. case DRM_FORMAT_YUV410:
  4970. case DRM_FORMAT_YVU410:
  4971. case DRM_FORMAT_YUV411:
  4972. case DRM_FORMAT_YVU411:
  4973. case DRM_FORMAT_YUV420:
  4974. case DRM_FORMAT_YVU420:
  4975. case DRM_FORMAT_YUV422:
  4976. case DRM_FORMAT_YVU422:
  4977. case DRM_FORMAT_YUV444:
  4978. case DRM_FORMAT_YVU444:
  4979. return 1;
  4980. default:
  4981. drm_fb_get_bpp_depth(format, &depth, &bpp);
  4982. return bpp >> 3;
  4983. }
  4984. }
  4985. EXPORT_SYMBOL(drm_format_plane_cpp);
  4986. /**
  4987. * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  4988. * @format: pixel format (DRM_FORMAT_*)
  4989. *
  4990. * Returns:
  4991. * The horizontal chroma subsampling factor for the
  4992. * specified pixel format.
  4993. */
  4994. int drm_format_horz_chroma_subsampling(uint32_t format)
  4995. {
  4996. switch (format) {
  4997. case DRM_FORMAT_YUV411:
  4998. case DRM_FORMAT_YVU411:
  4999. case DRM_FORMAT_YUV410:
  5000. case DRM_FORMAT_YVU410:
  5001. return 4;
  5002. case DRM_FORMAT_YUYV:
  5003. case DRM_FORMAT_YVYU:
  5004. case DRM_FORMAT_UYVY:
  5005. case DRM_FORMAT_VYUY:
  5006. case DRM_FORMAT_NV12:
  5007. case DRM_FORMAT_NV21:
  5008. case DRM_FORMAT_NV16:
  5009. case DRM_FORMAT_NV61:
  5010. case DRM_FORMAT_YUV422:
  5011. case DRM_FORMAT_YVU422:
  5012. case DRM_FORMAT_YUV420:
  5013. case DRM_FORMAT_YVU420:
  5014. return 2;
  5015. default:
  5016. return 1;
  5017. }
  5018. }
  5019. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  5020. /**
  5021. * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  5022. * @format: pixel format (DRM_FORMAT_*)
  5023. *
  5024. * Returns:
  5025. * The vertical chroma subsampling factor for the
  5026. * specified pixel format.
  5027. */
  5028. int drm_format_vert_chroma_subsampling(uint32_t format)
  5029. {
  5030. switch (format) {
  5031. case DRM_FORMAT_YUV410:
  5032. case DRM_FORMAT_YVU410:
  5033. return 4;
  5034. case DRM_FORMAT_YUV420:
  5035. case DRM_FORMAT_YVU420:
  5036. case DRM_FORMAT_NV12:
  5037. case DRM_FORMAT_NV21:
  5038. return 2;
  5039. default:
  5040. return 1;
  5041. }
  5042. }
  5043. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  5044. /**
  5045. * drm_format_plane_width - width of the plane given the first plane
  5046. * @width: width of the first plane
  5047. * @format: pixel format
  5048. * @plane: plane index
  5049. *
  5050. * Returns:
  5051. * The width of @plane, given that the width of the first plane is @width.
  5052. */
  5053. int drm_format_plane_width(int width, uint32_t format, int plane)
  5054. {
  5055. if (plane >= drm_format_num_planes(format))
  5056. return 0;
  5057. if (plane == 0)
  5058. return width;
  5059. return width / drm_format_horz_chroma_subsampling(format);
  5060. }
  5061. EXPORT_SYMBOL(drm_format_plane_width);
  5062. /**
  5063. * drm_format_plane_height - height of the plane given the first plane
  5064. * @height: height of the first plane
  5065. * @format: pixel format
  5066. * @plane: plane index
  5067. *
  5068. * Returns:
  5069. * The height of @plane, given that the height of the first plane is @height.
  5070. */
  5071. int drm_format_plane_height(int height, uint32_t format, int plane)
  5072. {
  5073. if (plane >= drm_format_num_planes(format))
  5074. return 0;
  5075. if (plane == 0)
  5076. return height;
  5077. return height / drm_format_vert_chroma_subsampling(format);
  5078. }
  5079. EXPORT_SYMBOL(drm_format_plane_height);
  5080. /**
  5081. * drm_rotation_simplify() - Try to simplify the rotation
  5082. * @rotation: Rotation to be simplified
  5083. * @supported_rotations: Supported rotations
  5084. *
  5085. * Attempt to simplify the rotation to a form that is supported.
  5086. * Eg. if the hardware supports everything except DRM_REFLECT_X
  5087. * one could call this function like this:
  5088. *
  5089. * drm_rotation_simplify(rotation, BIT(DRM_ROTATE_0) |
  5090. * BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_180) |
  5091. * BIT(DRM_ROTATE_270) | BIT(DRM_REFLECT_Y));
  5092. *
  5093. * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
  5094. * transforms the hardware supports, this function may not
  5095. * be able to produce a supported transform, so the caller should
  5096. * check the result afterwards.
  5097. */
  5098. unsigned int drm_rotation_simplify(unsigned int rotation,
  5099. unsigned int supported_rotations)
  5100. {
  5101. if (rotation & ~supported_rotations) {
  5102. rotation ^= BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y);
  5103. rotation = (rotation & DRM_REFLECT_MASK) |
  5104. BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
  5105. }
  5106. return rotation;
  5107. }
  5108. EXPORT_SYMBOL(drm_rotation_simplify);
  5109. /**
  5110. * drm_mode_config_init - initialize DRM mode_configuration structure
  5111. * @dev: DRM device
  5112. *
  5113. * Initialize @dev's mode_config structure, used for tracking the graphics
  5114. * configuration of @dev.
  5115. *
  5116. * Since this initializes the modeset locks, no locking is possible. Which is no
  5117. * problem, since this should happen single threaded at init time. It is the
  5118. * driver's problem to ensure this guarantee.
  5119. *
  5120. */
  5121. void drm_mode_config_init(struct drm_device *dev)
  5122. {
  5123. mutex_init(&dev->mode_config.mutex);
  5124. drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  5125. mutex_init(&dev->mode_config.idr_mutex);
  5126. mutex_init(&dev->mode_config.fb_lock);
  5127. mutex_init(&dev->mode_config.blob_lock);
  5128. INIT_LIST_HEAD(&dev->mode_config.fb_list);
  5129. INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  5130. INIT_LIST_HEAD(&dev->mode_config.connector_list);
  5131. INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  5132. INIT_LIST_HEAD(&dev->mode_config.property_list);
  5133. INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  5134. INIT_LIST_HEAD(&dev->mode_config.plane_list);
  5135. idr_init(&dev->mode_config.crtc_idr);
  5136. idr_init(&dev->mode_config.tile_idr);
  5137. ida_init(&dev->mode_config.connector_ida);
  5138. drm_modeset_lock_all(dev);
  5139. drm_mode_create_standard_properties(dev);
  5140. drm_modeset_unlock_all(dev);
  5141. /* Just to be sure */
  5142. dev->mode_config.num_fb = 0;
  5143. dev->mode_config.num_connector = 0;
  5144. dev->mode_config.num_crtc = 0;
  5145. dev->mode_config.num_encoder = 0;
  5146. dev->mode_config.num_overlay_plane = 0;
  5147. dev->mode_config.num_total_plane = 0;
  5148. }
  5149. EXPORT_SYMBOL(drm_mode_config_init);
  5150. /**
  5151. * drm_mode_config_cleanup - free up DRM mode_config info
  5152. * @dev: DRM device
  5153. *
  5154. * Free up all the connectors and CRTCs associated with this DRM device, then
  5155. * free up the framebuffers and associated buffer objects.
  5156. *
  5157. * Note that since this /should/ happen single-threaded at driver/device
  5158. * teardown time, no locking is required. It's the driver's job to ensure that
  5159. * this guarantee actually holds true.
  5160. *
  5161. * FIXME: cleanup any dangling user buffer objects too
  5162. */
  5163. void drm_mode_config_cleanup(struct drm_device *dev)
  5164. {
  5165. struct drm_connector *connector, *ot;
  5166. struct drm_crtc *crtc, *ct;
  5167. struct drm_encoder *encoder, *enct;
  5168. struct drm_framebuffer *fb, *fbt;
  5169. struct drm_property *property, *pt;
  5170. struct drm_property_blob *blob, *bt;
  5171. struct drm_plane *plane, *plt;
  5172. list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  5173. head) {
  5174. encoder->funcs->destroy(encoder);
  5175. }
  5176. list_for_each_entry_safe(connector, ot,
  5177. &dev->mode_config.connector_list, head) {
  5178. connector->funcs->destroy(connector);
  5179. }
  5180. list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  5181. head) {
  5182. drm_property_destroy(dev, property);
  5183. }
  5184. list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
  5185. head) {
  5186. plane->funcs->destroy(plane);
  5187. }
  5188. list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  5189. crtc->funcs->destroy(crtc);
  5190. }
  5191. list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
  5192. head_global) {
  5193. drm_property_unreference_blob(blob);
  5194. }
  5195. /*
  5196. * Single-threaded teardown context, so it's not required to grab the
  5197. * fb_lock to protect against concurrent fb_list access. Contrary, it
  5198. * would actually deadlock with the drm_framebuffer_cleanup function.
  5199. *
  5200. * Also, if there are any framebuffers left, that's a driver leak now,
  5201. * so politely WARN about this.
  5202. */
  5203. WARN_ON(!list_empty(&dev->mode_config.fb_list));
  5204. list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  5205. drm_framebuffer_free(&fb->base.refcount);
  5206. }
  5207. ida_destroy(&dev->mode_config.connector_ida);
  5208. idr_destroy(&dev->mode_config.tile_idr);
  5209. idr_destroy(&dev->mode_config.crtc_idr);
  5210. drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
  5211. }
  5212. EXPORT_SYMBOL(drm_mode_config_cleanup);
  5213. struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
  5214. unsigned int supported_rotations)
  5215. {
  5216. static const struct drm_prop_enum_list props[] = {
  5217. { DRM_ROTATE_0, "rotate-0" },
  5218. { DRM_ROTATE_90, "rotate-90" },
  5219. { DRM_ROTATE_180, "rotate-180" },
  5220. { DRM_ROTATE_270, "rotate-270" },
  5221. { DRM_REFLECT_X, "reflect-x" },
  5222. { DRM_REFLECT_Y, "reflect-y" },
  5223. };
  5224. return drm_property_create_bitmask(dev, 0, "rotation",
  5225. props, ARRAY_SIZE(props),
  5226. supported_rotations);
  5227. }
  5228. EXPORT_SYMBOL(drm_mode_create_rotation_property);
  5229. /**
  5230. * DOC: Tile group
  5231. *
  5232. * Tile groups are used to represent tiled monitors with a unique
  5233. * integer identifier. Tiled monitors using DisplayID v1.3 have
  5234. * a unique 8-byte handle, we store this in a tile group, so we
  5235. * have a common identifier for all tiles in a monitor group.
  5236. */
  5237. static void drm_tile_group_free(struct kref *kref)
  5238. {
  5239. struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
  5240. struct drm_device *dev = tg->dev;
  5241. mutex_lock(&dev->mode_config.idr_mutex);
  5242. idr_remove(&dev->mode_config.tile_idr, tg->id);
  5243. mutex_unlock(&dev->mode_config.idr_mutex);
  5244. kfree(tg);
  5245. }
  5246. /**
  5247. * drm_mode_put_tile_group - drop a reference to a tile group.
  5248. * @dev: DRM device
  5249. * @tg: tile group to drop reference to.
  5250. *
  5251. * drop reference to tile group and free if 0.
  5252. */
  5253. void drm_mode_put_tile_group(struct drm_device *dev,
  5254. struct drm_tile_group *tg)
  5255. {
  5256. kref_put(&tg->refcount, drm_tile_group_free);
  5257. }
  5258. /**
  5259. * drm_mode_get_tile_group - get a reference to an existing tile group
  5260. * @dev: DRM device
  5261. * @topology: 8-bytes unique per monitor.
  5262. *
  5263. * Use the unique bytes to get a reference to an existing tile group.
  5264. *
  5265. * RETURNS:
  5266. * tile group or NULL if not found.
  5267. */
  5268. struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
  5269. char topology[8])
  5270. {
  5271. struct drm_tile_group *tg;
  5272. int id;
  5273. mutex_lock(&dev->mode_config.idr_mutex);
  5274. idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
  5275. if (!memcmp(tg->group_data, topology, 8)) {
  5276. if (!kref_get_unless_zero(&tg->refcount))
  5277. tg = NULL;
  5278. mutex_unlock(&dev->mode_config.idr_mutex);
  5279. return tg;
  5280. }
  5281. }
  5282. mutex_unlock(&dev->mode_config.idr_mutex);
  5283. return NULL;
  5284. }
  5285. EXPORT_SYMBOL(drm_mode_get_tile_group);
  5286. /**
  5287. * drm_mode_create_tile_group - create a tile group from a displayid description
  5288. * @dev: DRM device
  5289. * @topology: 8-bytes unique per monitor.
  5290. *
  5291. * Create a tile group for the unique monitor, and get a unique
  5292. * identifier for the tile group.
  5293. *
  5294. * RETURNS:
  5295. * new tile group or error.
  5296. */
  5297. struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
  5298. char topology[8])
  5299. {
  5300. struct drm_tile_group *tg;
  5301. int ret;
  5302. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5303. if (!tg)
  5304. return ERR_PTR(-ENOMEM);
  5305. kref_init(&tg->refcount);
  5306. memcpy(tg->group_data, topology, 8);
  5307. tg->dev = dev;
  5308. mutex_lock(&dev->mode_config.idr_mutex);
  5309. ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
  5310. if (ret >= 0) {
  5311. tg->id = ret;
  5312. } else {
  5313. kfree(tg);
  5314. tg = ERR_PTR(ret);
  5315. }
  5316. mutex_unlock(&dev->mode_config.idr_mutex);
  5317. return tg;
  5318. }
  5319. EXPORT_SYMBOL(drm_mode_create_tile_group);