drm_crtc.c 160 KB

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