drm_crtc.c 103 KB

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