drm_crtc.c 82 KB

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