drm_crtc.c 151 KB

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