drm_crtc.c 138 KB

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