drm_crtc.c 160 KB

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