drm_crtc.c 124 KB

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