drm_crtc.c 95 KB

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