drm_crtc.c 141 KB

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