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