drm_crtc.c 99 KB

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